Facebook SDK 4 for Android - how to log out programmatically - android

Recently, Facebook released SDK 4 with new and cool updates. I tried to switch into SDK4 to use new features, however, I am struggling with the Login feature of Facebook.
So far, to log out Facebook programmatically, I used :
Session session = Session.getActiveSession();
session.closeAndClearTokenInformation();
But SDK4 seems not to support Session anymore, and in official docs, they mention:
There are two ways to implement Facebook login on Android:
LoginButton class - Which provides a button you can add to your UI. It follows the current access token and can log people in and out.
Well, seems there's no way to log out Facebook programmatically except using LoginButton.
Anyone have any idea, please share it here.

You can use LoginManager.getInstance().logOut();, even if you use LoginButton because
This UI element wraps functionality available in the LoginManager.
EDIT:
Just to mention that this works for Facebook SDK v4. I don't know if they will change it in the future.
#as batoutofhell mention, don't forget to put FacebookSdk.sdkInitialize(getApplicationContext()); to initialize the facebook sdk. Please see here for the details.

SDK4, if you want to completely de-couple, make sure you also remove the app from the user's facebook account. This method disconnects the user completely:
public void disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return; // already logged out
}
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}

You can use LoginManager.logOut()
Check out https://developers.facebook.com/docs/reference/android/current/class/LoginManager/

To handle it with the loginButton:
//Check if user is currently logged in
if (AccessToken.getCurrentAccessToken() != null && com.facebook.Profile.getCurrentProfile() != null){
//Logged in so show the login button
fbLogin.setVisibility(View.VISIBLE);
fbLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//log out
LoginManager.getInstance().logOut();
gotoLogin();
}
});
}

You can logout by using LoginManager but you have to use graph request also. I am talking about log out completely so, that next time you can login with different account.
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
SharedPreferences pref = DashBoard.this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();
LoginManager.getInstance().logOut();
Intent logoutint = new Intent(DashBoard.this,MainActivity.class);
logoutint.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logoutint);
}
}).executeAsync();
By the help of shared preferences here you can logout completely, and next time you can login with different account.

Frank version kotlin:
fun disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return // already logged out
}
GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/permissions/",
null,
HttpMethod.DELETE,
GraphRequest.Callback {
LoginManager.getInstance().logOut()
}).executeAsync()
}

Related

Sing out Facebook firebase auth completely [duplicate]

i´m not able to find any solution that solves my Problem.
I use Firebase and Facebook to sign up to my app.
When I first sign up to my app, i get the following screen:
Facebook First Login-Screen
I sign out from Firebase and Facebook using the following Lines of Code:
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
When I´m trying after that to sign in with Facebook again, I get the following Screen:
Facebook Second Login-Screen
I´m only able to continue with the Account I used before.
I want to come back to the first Screen, where I have to enter E-Mail an Password so I could Sign in with another Facebook Account if I want.
On Stackoverflow I found the following promising Code:
FacebookSdk.sdkInitialize(getApplicationContext());
if (AccessToken.getCurrentAccessToken() != null) {
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
AccessToken.setCurrentAccessToken(null);
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
But this solved also not my Problem.
After that I get the following Screen:
Facebook alternate Second Login-Screen
I would by very thankful for any help and sorry for any English mistakes.
Use below code before you click on facebook login button
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
accessTokenTracker.stopTracking();
DebugLog.infoLog("Token Changed Called");
if (currentAccessToken == null) {
LoginManager.getInstance().logInWithReadPermissions(mActivity, Arrays.asList(permissionList));;
}
}
};
AccessToken.refreshCurrentAccessTokenAsync();
After above code just call login code as mentioned below (use handler to call below code with 1 milliseconds wait time):
LoginManager.getInstance().logInWithReadPermissions(mActivity, Arrays.asList(permissionList));

Facebook android sdk not clear previous login at logout

My app has facebook login integration in it, And i found one issue while re-login, When i tried to logout from facebook and after that re-login i am getting previous user auto login in my app but what i need is to login as new user. Don't Know what's Going wrong
My Logout code is
val parameters = Bundle()
val request = GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/permissions/",
parameters,
HttpMethod.DELETE,
GraphRequest.Callback {
// Insert your code here
LoginManager.getInstance().logOut()
})
request.executeAsync()
You need to initialize the FacebookSdk in the logout activity before calling logOut().
LoginManager.getInstance().logOut();,will work
but dont forgot to put FacebookSdk.sdkInitialize(getApplicationContext()); inside your LogoutActivity onCreate
For Logout from Facebook only need to use:
LoginManager.getInstance().logOut();
In facebook SDK,if you want to completely de-couple, make sure you also remove the app from the user's facebook account. This method disconnects the user completely:
public void logoutFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return; // user already logged out
}
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
also use this FacebookSdk.sdkInitialize(getApplicationContext()); to initialize the facebook sdk.
Hope it will help you!!

Android Facebook login always ask for user after logout

I am implementing a Facebook login with Facebook SDK on Android.
compile 'com.facebook.android:facebook-android-sdk:4.+'
I'm logging in with the user as
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
..
}
And for log out, I use my own log out button and log out the user programmatically:
LoginManager.getInstance().logOut();
My question is:
After log out, when I click on Continue with Facebook, the previous user information pops up to Continue as XY. I don't want this. I want to ask for email and password again, every time, if somebody wants to log in after log out. How can I do this?
Actually i found the solution. I changed the login behavior for the FB login button, for this i used:
loginButton.setLoginBehavior(LoginBehavior.WEB_ONLY);
So every time it pops up the WEB view for login button.
I want to ask for email, password again every time if somebody wants
to log in after log out.
try this in your onCreate()
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
updateWithToken(AccessToken.getCurrentAccessToken()); //add this method
Now in updateWithToken() method logout the user from previous session
private void updateWithToken(AccessToken currentAccessToken) {
if (currentAccessToken != null) {
LoginManager.getInstance().logOut();
} else {
}
}
EDIT
if you want to completely disconnect user from facebook login use:
public void disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return; // already logged out
}
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
To really disconnect from facebook with the android SDk and avoid auto login after you must use
val accessToken = AccessToken.getCurrentAccessToken()
val request = GraphRequest.newDeleteObjectRequest(accessToken, "me/permissions", { response ->
LoginManager.getInstance().logOut() //not really needed i think
})
request.executeAsync()
In response you have a responseCode == 200 if OK

Revoking android app permission not clearing facebook access token

I have a activity called StarterActivity which is the launcher activity of my android application. I have provided a logout menu option on press of which I am revoking all apps permissions. I verified all permissions are getting revoked and my app is no longer listed in https://www.facebook.com/settings?tab=applications
However the access token does not get cleared.
switch(item.getItemId())
{
case R.id.action_logout:
GraphRequest delPermRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), "/{user-id}/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
if(graphResponse!=null){
FacebookRequestError error =graphResponse.getError();
if(error!=null){
Log.e(TAG, error.toString());
}else {
finish();
}
}
}
});
Log.d(TAG,"Executing revoke permissions with graph path" + delPermRequest.getGraphPath());
delPermRequest.executeAsync();
break;
}
I want to relaunch my StarterActivity Intent again on logout.
I added
startActivity(new Intent(getApplicationContext(),StarterActivity.class));
after clearing permissions. But neither AccessToken.getCurrentAccessToken() or Profile.getCurrentProfile() is null. Perhaps getting cashed?
I also tried
AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(
AccessToken oldAccessToken,
AccessToken currentAccessToken) {
Log.d(TAG,"Access token changed");
if (currentAccessToken == null){
//User logged out
startActivity(new Intent(getApplicationContext(),StarterActivity.class));
}
}
};
But none of them seem to work. Access token is not cleared. How can I invalidate this data if cashed? I was hoping for it to get cleared on revoking permissions? Or is there a neater way to logout?
I am using SDK 4.x. More details on perm - https://developers.facebook.com/docs/graph-api/reference/user/permissions
What finally worked was
LoginManager.getInstance().logOut();
What it internally does is setting each access token and profile to null
public void logOut() {
AccessToken.setCurrentAccessToken((AccessToken)null);
Profile.setCurrentProfile((Profile)null);
}
Just revoking permission will not do. You can either manually set token and profile as null or use above API.

How to programmatically log out from Facebook SDK 3.0 without using Facebook login/logout button?

The title says it all. I'm using a custom button to fetch the user's facebook information (for "sign up" purposes). Yet, I don't want the app to remember the last registered user, neither the currently logged in person via the Facebook native app. I want the Facebook login activity to pop up each time. That is why I want to log out any previous users programmatically.
How can I do that? This is how I do the login:
private void signInWithFacebook() {
SessionTracker sessionTracker = new SessionTracker(getBaseContext(), new StatusCallback()
{
#Override
public void call(Session session, SessionState state, Exception exception) {
}
}, null, false);
String applicationId = Utility.getMetadataApplicationId(getBaseContext());
mCurrentSession = sessionTracker.getSession();
if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
sessionTracker.setSession(null);
Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
mCurrentSession = session;
}
if (!mCurrentSession.isOpened()) {
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(RegisterActivity.this);
if (openRequest != null) {
openRequest.setPermissions(null);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
mCurrentSession.openForRead(openRequest);
}
}else {
Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
fillProfileWithFacebook( user );
}
});
}
}
Ideally, I would make a call at the beginning of this method to log out any previous users.
Update for latest SDK:
Now #zeuter's answer is correct for Facebook SDK v4.7+:
LoginManager.getInstance().logOut();
Original answer:
Please do not use SessionTracker. It is an internal (package private) class, and is not meant to be consumed as part of the public API. As such, its API may change at any time without any backwards compatibility guarantees. You should be able to get rid of all instances of SessionTracker in your code, and just use the active session instead.
To answer your question, if you don't want to keep any session data, simply call closeAndClearTokenInformation when your app closes.
This method will help you to logout from facebook programmatically in android
/**
* Logout From Facebook
*/
public static void callFacebookLogout(Context context) {
Session session = Session.getActiveSession();
if (session != null) {
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
} else {
session = new Session(context);
Session.setActiveSession(session);
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
}
Since Facebook's Android SDK v4.0 (see changelog) you need to execute the following:
LoginManager.getInstance().logOut();
Here is snippet that allowed me to log out programmatically from facebook. Let me know if you see anything that I might need to improve.
private void logout(){
// clear any user information
mApp.clearUserPrefs();
// find the active session which can only be facebook in my app
Session session = Session.getActiveSession();
// run the closeAndClearTokenInformation which does the following
// DOCS : Closes the local in-memory Session object and clears any persistent
// cache related to the Session.
session.closeAndClearTokenInformation();
// return the user to the login screen
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
// make sure the user can not access the page after he/she is logged out
// clear the activity stack
finish();
}
Since Facebook's Android SDK v4.0 you need to execute the following:
LoginManager.getInstance().logOut();
This is not sufficient. This will simply clear cached access token and profile so that AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile() will now become null.
To completely logout you need to revoke permissions and then call LoginManager.getInstance().logOut();. To revoke permission execute following graph API -
GraphRequest delPermRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), "/{user-id}/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
if(graphResponse!=null){
FacebookRequestError error =graphResponse.getError();
if(error!=null){
Log.e(TAG, error.toString());
}else {
finish();
}
}
}
});
Log.d(TAG,"Executing revoke permissions with graph path" + delPermRequest.getGraphPath());
delPermRequest.executeAsync();
Session class has been removed on SDK 4.0. The login magement is done through the class LoginManager. So:
mLoginManager = LoginManager.getInstance();
mLoginManager.logOut();
As the reference Upgrading to SDK 4.0 says:
Session Removed - AccessToken, LoginManager and CallbackManager classes supercede and replace functionality in the Session class.
Yup, As #luizfelippe mentioned Session class has been removed since SDK 4.0. We need to use LoginManager.
I just looked into LoginButton class for logout. They are making this kind of check. They logs out only if accessToken is not null. So, I think its better to have this in our code too..
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if(accessToken != null){
LoginManager.getInstance().logOut();
}
private Session.StatusCallback statusCallback = new SessionStatusCallback();
logout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Session.openActiveSession(this, true, statusCallback);
}
});
private class SessionStatusCallback implements Session.StatusCallback {
#Override
public void call(Session session, SessionState state,
Exception exception) {
session.closeAndClearTokenInformation();
}
}
Facebook provides two ways to login and logout from an account. One is to use LoginButton and the other is to use LoginManager. LoginButton is just a button which on clicked, the logging in is accomplished. On the other side LoginManager does this on its own. In your case you have use LoginManager to logout automatically.
LoginManager.getInstance().logout() does this work for you.

Categories

Resources