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?
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.
This answer helped me in similar situation.
https://stackoverflow.com/a/31625256/1987045
Call LoginManager.getInstance().logOut();
before attempting a sign in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With