I have an Android app and I am trying to use Facebook's SDK (version 4.1.0) to get a token and log in. Here is my code:
public class LoginActivity extends Activity { private CallbackManager callbackManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { AccessToken accessToken = loginResult.getAccessToken(); Log.v(TAG, "Facebook login was successful"); String authToken = accessToken.getToken(); // User authToken here: } @Override public void onCancel() { Log.v(TAG, "Facebook login was canceled"); } @Override public void onError(FacebookException e) { Log.e(TAG, "Facebook login failed: " + e.getMessage()); } }); Button facebook_button = (Button) findViewById(R.id.fbButton); facebook_button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { LoginManager.getInstance().logInWithReadPermissions(getActivity(), Arrays.asList("public_profile")); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } }
The code switched to the Facebook app and back and OnActivityResult()
is called. However, every time the callback method that is called is onCancel()
. Note that I am not using the LoginButton
provided by Facebook, and I have my own button (although I tried that approach and the result was the same). I double and triple checked my app ID and the keyhash generated by the app and they look correct too. So, I don't know what else may be wrong. Any help at this point is greatly appreciated.
The Facebook SDK is a set of software components that developers can include in their mobile app to understand how people use the app, run optimized marketing campaigns and enable Facebook login and social sharing.
Testing Your CallbackGo to your Facebook profile's Apps and Websites settings tab: https://www.facebook.com/settings?tab=applications. Click the View Removed Apps and Websites link. In the popup, click the View button to the right of the application. In the window, appeared click Send Request to trigger your callback.
yes i was facing the same issue, resolved it by using the below code just before login
LoginManager.getInstance().logOut();
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