Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook LoginButton Callback jumps to canceled

I have the following code. It is a simple test of the callback function of the LoginButton in Facebook's Android API.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    callbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");
    loginButton.setReadPermissions("email");
    loginButton.setReadPermissions("public_profile");
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.e("SUCCESS", "LOGIN SUCCESSFUL");
        }

        @Override
        public void onCancel() {
            Log.e("CANCEL", "Cancelled");
        }

        @Override
        public void onError(FacebookException e) {
            Log.e("ERROR", "Facebook Exception");
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

The only problem is that whenever I click the loginButton, a dialog appears and disappears for a brief moment, and then the logcat prints out the "CANCEL" text. I cannot seem to fix this error, and I'm not clicking cancel, because that dialog simply shows up and immediately disappears.

Please help me fix this?

like image 280
theLonelyYak Avatar asked Apr 18 '15 02:04

theLonelyYak


2 Answers

I encountered this issue.

It seems to be the case that if you attempt to login (using logInWithReadPermissions or logInWithPublishPermissions) when you already have an access token (with all of the required permissions) then the onCancel callback is executed.

That is to say you should not be logging the user in because the user is already logged in. Check for this first.

like image 116
Thomas Clowes Avatar answered Sep 27 '22 22:09

Thomas Clowes


This answer helped me in similar situation.

https://stackoverflow.com/a/31625256/1987045

Call LoginManager.getInstance().logOut(); before attempting a sign in.

like image 26
rahulrvp Avatar answered Sep 27 '22 23:09

rahulrvp