Android的 - 清除Facebook的访问令牌令牌、Android、Facebook

2023-09-06 23:06:44 作者:该用户不存在

我有一个 SettingsActivity ,其中有几个选择,包括寻找Facebook好友和注销。因此,当用户选择找到她的Facebook上的朋友,她会被送到哪里我让她记录在她的Facebook帐户,并挽救她的访问令牌另一项活动。然后,当她选择退出,她的访问令牌将被清除。问题是,我的注销方法不是写在与我创建的会话一个相同的活动,所以,当我尝试这样做:

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的点 NullPointerException异常。然后我尝试:

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.

- 编辑 -

我觉得有,我用它来打开会话出毛病了活动。我跟着Facebook的 GraphAPISample ,这是我做了什么

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

当我尝试把在吐司 的createSession()检查创建 activeSession ,它总是表明会话关闭。这很奇怪,因为 showFriends()的方法还是触发,即使我删除并重新安装应用程序,它仍然表现出同样的好友列表。我真的在这里感到困惑。

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.

推荐答案

在的Facebook SDK V4 我们可以使用注销

LoginManager.getInstance().logOut();

注意,不要忘了初始化SDK这样

note that don't forget to initialize sdk like this

FacebookSdk.sdkInitialize(getApplicationContext());