Android - Clear Facebook access token

不羁岁月 提交于 2019-12-19 11:48:30

问题


I have a SettingsActivity where there are several options including finding Facebook friends and logging out. So when a user chooses to find her Facebook friends, she will be sent to another activity where I let her logging in with her Facebook account and save her access token. And then when she chooses to log out, her access token will be cleared. The problem is, my logout method is not written in the same activity with the one I created the session, so when I tried this:

    Session session = Session.getActiveSession();
    if (!session.isClosed()) {
        session.closeAndClearTokenInformation();
    }

Logcat points NullPointerException. Then I tried:

Session.setActiveSession(null);

And that doesn't work either (friends of the user logged in earlier are still shown, instead of asking the new user to log in).

So what should I do to clear the obtained token? Thank you in advance.

-EDIT-

I think there's something wrong with the activity which I use to open the session. I followed Facebook's GraphAPISample and this is what I have done:

public class FacebookFriendsActivity extends FragmentActivity{
    private BroadcastReceiver broadcast;
    public static Session session;
    private boolean pendingRequest;
    static final String PENDING_REQUEST_BUNDLE_KEY = "PendingRequest";

    private DatabaseHandler db;
    private FindFacebookFriends task;
    private ProgressBar progress;
    private TextView text;
    private Button retryBtn;
    private ListView userLayout;
    private ArrayList<User> userList;
    private FollowAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.facebook_friends);
        // ... set views 

        session = createSession();
        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

        showFriends();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (session.onActivityResult(this, requestCode, resultCode, data) &&
                pendingRequest &&
                session.getState().isOpened()) {
            showFriends();
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        pendingRequest = savedInstanceState.getBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
    }

    private void showFriends(){
        task = new FindFacebookFriends();
        task.execute(session.getAccessToken());

    }

    private Session createSession() {
        Session activeSession = Session.getActiveSession();
        if (activeSession == null || activeSession.getState().isClosed()) {
            activeSession = new Session.Builder(this).setApplicationId(getString(R.string.fb_app_id)).build();
            Session.setActiveSession(activeSession);
            new SaveFacebookId().execute(activeSession.getAccessToken());
        }

        return activeSession;
    }
}

When I try putting a Toast in createSession() to check the state of the created activeSession, it always shows that the session is closed. It's weird because the showFriends() method is still triggered and even after I delete and install the app again, it still show the same friend list. I'm really confused here.


回答1:


in Facebook SDK v4 we can log out using

LoginManager.getInstance().logOut();

note that don't forget to initialize sdk like this

FacebookSdk.sdkInitialize(getApplicationContext());



回答2:


Using Facebook SDK v4 you can do the following:

if (AccessToken.getCurrentAccessToken() != null) {
    LoginManager.getInstance().logOut();
}

You can take a look at the LoginManager class.




回答3:


Do like this

try {
Session.getActiveSession().closeAndClearTokenInformation();
} catch (Throwable e) {
e.printStackTrace();
}



回答4:


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();    
}
}



回答5:


Try this below code for session clear in facebook integration android.

Put below method in your activity and call before the login to facebook.

public static void fbClearToken(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
    }

}

For me it's working... Hope this is helpful for you...




回答6:


You should pass session variable through Parcelable interface and clear session through that session variable. Pass facebook session like this : Passing a Facebook session across activities



来源:https://stackoverflow.com/questions/18529874/android-clear-facebook-access-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!