Got this error while trying to integrate Facebook

2019-08-14 04:12发布

问题:

mfacebook_Callback_Manager = CallbackManager.Factory.create();
        Collection<String> permissions = Arrays.asList("public_profile,email");
        LoginManager.getInstance().logInWithReadPermissions(MainActivity.this,
permissions);
        LoginManager.getInstance().registerCallback(mfacebook_Callback_Manager,
new FacebookCallback<LoginResult>() {.......}

Error occurs in callback of loginmanager.

Stack Trace:

07-12 21:25:10.712 2107-2107/com.factory.fbsample.dev W/System.err: at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:191 07-12 21:25:10.712 217-2107/com.factory.fbsample.dev W/System.err: at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:160 07-12 21:25:10.712 217-2107/com.factory.fbsample.dev W/System.err: at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.jva:82 07-12 21:25:10.712 217-210/com.factory.fbsample.dev W/System.err: at com.factory.fbsample.activities.LoginActivity.onActivityResult(LogiActivty.java:272 07-12 21:25:10.712 217-210/th.co.hond.hconnect.dev W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:5322 07-12 21:5:10.712 2107-2107/com.factory.fbsample.dev W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:3363 07-12 21:5:10.712 2107-2107/com.factory.fbsample.dev W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410 07-12 21:5:10.712 2107-2107/com.factory.fbsample.dev W/System.err: at android.app.ActivityThread.access$1100(ActivityThread.java:141 07-12 2125:10.712 2107-2107/com.factory.fbsample.dev W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:130 07-12 215:10.712 2107-2107/com.factory.fbsample.dev W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99) 07-12 215:10.720 2107-2107/com.factory.fbsample.dev W/System.err: at android.os.Looper.loop(Looper.java:137) 07-12 215:10.720 2107-2107/com.factory.fbsample.dev W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5103) 07-12 215:10.720 2107-2107/com.factory.fbsample.dev W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) 07-12 215:10.720 2107-2107/com.factory.fbsample.dev W/System.err: at java.lang.reflect.Method.invoke(Method.java:525) 07-12 215:10.720 2107-2107/com.factory.fbsample.dev W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteIn.java:737 07-12 215:10.720 107-2107/com.factory.fbsample.dev W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 07-12 21:25:10.720 2107-2107/com.factory.fbsample.dev W/System.err: at dalvik.system.NativeStart.main(Native Method)

回答1:

You should post the full error (including a stack trace) as part of your question.

Also, your permissions are constructed incorrectly. You should do this instead:

Collection<String> permissions = Arrays.asList("public_profile","email");

This is documented here and here.



回答2:

you can use it as

login_button.setReadPermissions(Arrays.asList("public_profile","email"));
            login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
            { 
                @Override 
                public void onSuccess(LoginResult loginResult)
                { 
                    login_button.setVisibility(View.GONE);

                    GraphRequest graphRequest   =   GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
                    { 
                        @Override 
                        public void onCompleted(JSONObject object, GraphResponse response)
                        { 
                            Log.d("JSON", ""+response.getJSONObject().toString());

                            try 
                            { 
                                email       =   object.getString("email");
                                name        =   object.getString("name");
                                first_name  =   object.optString("first_name");
                                last_name   =   object.optString("last_name");

                                tvEmail.setText(email);
                                tvfirst_name.setText(first_name);
                                tvlast_namee.setText(last_name);
                                tvfull_name.setText(name);
                                LoginManager.getInstance().logOut(); 
                            } 
                            catch (JSONException e)
                            { 
                                e.printStackTrace();
                            } 
                        } 
                    }); 

                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,first_name,last_name,email");
                    graphRequest.setParameters(parameters);
                    graphRequest.executeAsync();
                } 

                @Override 
                public void onCancel() 
                { 

                } 

                @Override 
                public void onError(FacebookException exception)
                { 

                } 
            }); 

or you can follow this link as well facebook login