如何以编程方式从Facebook SDK 3.0注销而不使用Facebook登录/注销按钮?

时间:2013-01-14 22:43:00

标签: android facebook facebook-graph-api facebook-android-sdk

标题说明了一切。我正在使用自定义按钮来获取用户的Facebook信息(用于“注册”目的)。然而,我不希望应用程序记住最后一个注册用户,也不想通过Facebook本机应用程序当前登录的用户。我希望每次都能弹出Facebook登录活动。这就是为什么我想以编程方式注销任何以前的用户。

我该怎么做?这就是我登录的方式:

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

理想情况下,我会在此方法的开头拨打电话,注销以前的所有用户。

8 个答案:

答案 0 :(得分:157)

更新最新SDK:

现在@ zeuter的答案对于Facebook SDK v4.7 +是正确的:

  

LoginManager.getInstance().logOut();

原始回答:

请不要使用SessionTracker。它是一个内部(包私有)类,并不打算作为公共API的一部分使用。因此,其API可能随时更改,无任何向后兼容性保证。您应该能够在代码中删除所有SessionTracker实例,而只需使用活动会话。

要回答您的问题,如果您不想保留任何会话数据,只需在应用关闭时致电closeAndClearTokenInformation

答案 1 :(得分:87)

此方法将帮助您以编程方式在android

中从facebook注销
/**
 * 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

    }

}

答案 2 :(得分:65)

自Facebook的Android SDK v4.0(参见changelog)起,您需要执行以下操作:

LoginManager.getInstance().logOut();

答案 3 :(得分:27)

这是允许我从facebook以编程方式注销的片段。如果您发现我可能需要改进的任何内容,请告诉我。

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

答案 4 :(得分:12)

自Facebook的Android SDK v4.0起,您需要执行以下操作:

LoginManager.getInstance().logOut();

这还不够。这将简单地清除缓存的访问令牌和配置文件,以便AccessToken.getCurrentAccessToken()Profile.getCurrentProfile()现在变为空。

要完全注销,您需要撤消权限,然后调用LoginManager.getInstance().logOut();。要撤消权限,请执行以下图表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();

答案 5 :(得分:3)

是的,正如@luizfelippe提到的会话类已被删除,因为 SDK 4.0。我们需要使用LoginManager。

我只是查看 LoginButton 类进行注销。他们正在进行这种检查。仅当 accessToken 不为空时,它们才会注销。所以,我认为在我们的代码中也更好。

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

Facebook提供了两种从帐户登录和注销的方法。一种是使用LoginButton,另一种是使用LoginManager。 LoginButton只是一个按钮,单击该按钮即可完成登录。另一方面,LoginManager自行完成此操作。在您的情况下,您已使用LoginManager自动注销。

LoginManager.getInstance().logout()这对您有用。