其他分享
首页 > 其他分享> > Android-清除Facebook访问令牌

Android-清除Facebook访问令牌

作者:互联网

我有一个SettingsActivity,其中有多个选项,包括查找Facebook朋友和注销.因此,当用户选择查找她的Facebook朋友时,她将被发送到另一个活动,在该活动中,我让她使用她的Facebook帐户登录并保存她的访问令牌.然后,当她选择注销时,她的访问令牌将被清除.问题是,我的注销方法与创建会话的方法不在同一活动中,因此当我尝试这样做时:

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

Logcat指向NullPointerException.然后我尝试了:

Session.setActiveSession(null);

而且这也不起作用(仍然显示先前登录用户的朋友,而不是要求新用户登录).

那么我应该怎么做才能清除获得的令牌?先感谢您.

-编辑-

我认为我用来打开会话的活动有问题.我遵循了Facebook的GraphAPISample,这就是我所做的:

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

当我尝试将Toast放入createSession()中以检查所创建的activeSession的状态时,它始终显示该会话已关闭.这很奇怪,因为showFriends()方法仍然被触发,即使我再次删除并安装该应用程序后,它仍然显示相同的朋友列表.我真的很困惑.

解决方法:

在Facebook SDK v4中,我们可以使用注销

LoginManager.getInstance().logOut();

请注意,不要忘了像这样初始化sdk

FacebookSdk.sdkInitialize(getApplicationContext());

标签:android,facebook,facebook-graph-api,facebook-android-sdk
来源: https://codeday.me/bug/20191009/1879137.html