简体   繁体   中英

Android Facebook integration issue (fb sdk 4)

I did facebook login implementation in my app. So that code is working on the first time it retrieved the user details from the facebook but when I use fb login for second time the oncancel part is executed, again if I clear app data and login using fb again it's worked again.

This is my code for registering app with facebook.

protected void Registerwithfb()
    {
        // TODO Auto-generated method stub
        callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile,user_about_me, email, user_birthday, user_friends"));


        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {


            @Override
            public void onSuccess(LoginResult result) {
                // TODO Auto-generated method stub
                // TODO Auto-generated method stub

                if (AccessToken.getCurrentAccessToken() != null) {

                    GraphRequest request = GraphRequest.newMeRequest(result.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {

                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            // TODO Auto-generated method stub
                            JSONObject json = response.getJSONObject();
                            try {
                                if (json != null) {
                                    String name = json.getString("name");
                                    String id = json.getString("id");


                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }


                    });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "name,email,first_name,last_name");
                    //  Toast.makeText(getApplicationContext(), parameters.toString(), Toast.LENGTH_LONG).show();
                    request.setParameters(parameters);
                    request.executeAsync();
                }


            }

            @Override
            public void onCancel() {
                // TODO Auto-generated method stub
                System.out.println("cancelled");

            }

            @Override
            public void onError(FacebookException error) {
                // TODO Auto-generated method stub
                System.out.println(error);

            }
        });


    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


                //super.onActivityResult(requestCode, resultCode, data);
            // facebook.authorizeCallback(requestCode, resultCode, data);
            //callbackManager.onActivityResult(requestCode, resultCode,data);
            super.onActivityResult(requestCode, resultCode, data);
            callbackManager.onActivityResult(requestCode, resultCode, data);


            }
protected void Registerwithfb()
        {
            // TODO Auto-generated method stub
            callbackManager = CallbackManager.Factory.create();

    //I simply added this line 
LoginManager.getInstance().logOut();

        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile,user_about_me, email, user_birthday, user_friends"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM