I can’t sign in with my Facebook credentials after signing out
I have an Android app that allows users to log in with their Facebook credentials. Once they log out for the first time, they can’t log in again. From Applications page in user settings After clearing the permissions of the app on Facebook’s website, the login works fine.
I logged in using LoginButton
.
You need an activity that has access to login information, extend this AuthActivity
Activity.
public class AuthActivity extends Activity {
private AuthUtils authUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
authUtils = new AuthUtils(this);
authUtils.onCreate(savedInstanceState);
}
public AuthUtils getAuthUtils() {
return authUtils;
}
@Override
public void onStart() {
super.onStart();
authUtils.onStart();
}
@Override
public void onStop() {
super.onStop();
authUtils.onStop();
}
@Override
public void onResume() {
super.onResume();
authUtils.onResume();
}
@Override
public void onPause() {
super.onPause();
authUtils.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
authUtils.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
authUtils.onSaveInstanceState(outState);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
authUtils.onActivityResult(requestCode, resultCode, data);
}
}
AuthUtils looks like this. I removed everything related to Google+ sign-in and everything unrelated to the sign-in process, such as saving user information using my app preferences.
public class AuthUtils implements ConnectionCallbacks, OnConnectionFailedListener {
private final Activity activityContext;
private UiLifecycleHelper facebookUiHelper;
public AuthUtils(Activity context) {
this.activityContext = context;
facebookUiHelper = new UiLifecycleHelper(context, facebookStatusCallback);
}
public void signInFacebook() {
LoginButton facebookLoginButton = new LoginButton(activityContext);
facebookLoginButton.setReadPermissions(Arrays.asList("email"));
facebookLoginButton.performClick();
}
public void signOutFacebook() {
Session facebookSession = Session.getActiveSession();
if(facebookSession != null) {
facebookSession.closeAndClearTokenInformation();
}
}
private StatusCallback facebookStatusCallback = new StatusCallback() {
@Override
public void call(final Session session, SessionState state, Exception exception) {
if(state.isOpened()) {
Request.newMeRequest(session, new GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
String email = "";
if(user.asMap().containsKey("email")) {
email = user.getProperty("email").toString();
} else {
// ... not related to login
}
Some actions here, not related to login.
}
});
} else if(state.isClosed()) {
// ... not related to login
}
}
};
public void onCreate(Bundle savedInstanceState) {
facebookUiHelper.onCreate(savedInstanceState);
}
public void onResume() {
facebookUiHelper.onResume();
}
public void onPause() {
facebookUiHelper.onPause();
}
public void onDestroy() {
facebookUiHelper.onDestroy();
}
public void onSaveInstanceState(Bundle outState) {
facebookUiHelper.onSaveInstanceState(outState);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
facebookUiHelper.onActivityResult(requestCode, resultCode, data);
}
}
Solution
I had the same issue even while running Facebook’s sample app. I solved this problem by providing Facebook with my default signature key: both in the developer settings of my sample app, https://developers.facebook.com/settings/developer/sample-app/。 Then in the app settings for the Android platform.
Facebook suggests a simple way to get the default key, which can be > find the method under Troubleshooting. The code to do this when running the Hello Facebook sample application is provided below.
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.facebook.samples.hellofacebook",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
Note: When you publish your app, you should not use the default key, but instead use your own key to generate and sign the app.