I'm upgrading my use of the Facebook SDK to the latest version. The following code is prety much lifted line by line from Facebook's own examples, which can be found here: https://developers.facebook.com/docs/facebook-login/android/v2.3
import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import com.facebook.CallbackManager; import com.facebook.FacebookCallback; import com.facebook.FacebookException; import com.facebook.FacebookSdk; import com.facebook.login.LoginManager; import com.facebook.login.LoginResult; import java.util.Arrays; public class TestFb extends FragmentActivity{ CallbackManager callbackManager; FacebookController fbController; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); fbController = FacebookController.getInstance(this); FacebookSdk.sdkInitialize(getApplicationContext()); final LoginManager loginManager = LoginManager.getInstance(); callbackManager = CallbackManager.Factory.create(); loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { String userId = loginResult.getAccessToken().getUserId(); String token = loginResult.getAccessToken().getToken(); Log.d("fb", "ID: " + userId + " Token: "+ token); } @Override public void onCancel() { finish(); } @Override public void onError(FacebookException e) { Log.d("fb", "Exception: " + e.getMessage()); } }); loginManager.logInWithReadPermissions(this, Arrays.asList(new String[]{"email", "user_likes"})); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } }
The problem is that this does not work when the user has already logged in to the facebook app on his phone. In that case, this gives a "User logged in as different Facebook user" error.
I would think that given that there already is an active Facebook account on the device, a new one cannot be created. But if that was the case, should I get the user's ID and token from the existing account? I couldn't seem to be able to find any mention of this issue on Facebook's documentation pages.
To use the Facebook SDK in an Android Studio project, add the SDK as a build dependency and import the SDK. Go to Android Studio | New Project | Minimum SDK. Select API 15: Android 4.0. 3 (IceCreamSandwich) or higher and create your new project.
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. This course helps you understand the purpose of the Facebook SDK and App Events for Android and iOS.
The Facebook SDK for Android gives you access to the following features: Facebook Login — A secure and convenient way for people to log into your app or website by using their Facebook credentials. Sharing — Enable people to post to Facebook from your app. People can share, send a message, and share to stories.
If you want to log in with the user that is now logged in on the Facebook app and you still have a token valid for a previous user, you can detect the error and log out in your FacebookCallback like this:
@Override public void onError(FacebookException e) { if (e instanceof FacebookAuthorizationException) { if (AccessToken.getCurrentAccessToken() != null) { LoginManager.getInstance().logOut(); } } }
Then you can log in again using the LoginManager.
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