如何获取当前访问令牌在Facebook的Andr​​oid

如何获取当前访问令牌在Facebook的Andr​​oid

本文介绍了如何获取当前访问令牌在Facebook的Andr​​oid SDK中V4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查用户是否登录或不属于主FragmentActivity不过虽然用户登录

返回null。在下面的code我究竟做错了什么?

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    上下文=这一点;
    FacebookSdk.sdkInitialize(上下文);
    callbackManager = CallbackManager.Factory.create();
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    如果(accessToken!= NULL){
        如果(!accessToken.isExpired()){

        }
    }
}
 

解决方案

用户如何登录,通过LoginButton?如果是的话,它会发生在他们点击登录按钮,让你检查哪里是太早了。

如果他们已经登录,则该访问令牌将被从磁盘反序列化,但是这是一个异步操作,使得UI线程不会被阻断。你选择了它您可以初始化SDK之后,以及异步操作可能尚未完成。我建议你​​使用AccessTokenTracker的AccessToken加载时作出回应。下面是一个使用了AccessTokenTracker供您参考的样本:https://github.com/facebook/facebook-android-sdk/blob/b384c0655fe96db71229bfdcb981a522f3f1e675/samples/Scrumptious/src/com/facebook/scrumptious/usersettings/UserSettingsFragment.java#L75

I want to check whether the user is logged in or not within the main FragmentActivity but although the user is logged in

returns null.What am I doing wrong in the code below ?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;
    FacebookSdk.sdkInitialize(context);
    callbackManager = CallbackManager.Factory.create();
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    if (accessToken != null) {
        if (!accessToken.isExpired()) {

        }
    }
}
解决方案

How is the user logging in, via the LoginButton? If so, it'll happen after they click the login button, so where you're checking is too early.

If they already logged in, then the access token is going to be deserialized from disk, however this is an async operation so that the UI thread is not blocked. You check it right after you initialize the Sdk, and the async operation is probably not completed yet. I suggest you use the AccessTokenTracker to respond when the AccessToken is loaded. Here's a sample that uses the AccessTokenTracker for your reference: https://github.com/facebook/facebook-android-sdk/blob/b384c0655fe96db71229bfdcb981a522f3f1e675/samples/Scrumptious/src/com/facebook/scrumptious/usersettings/UserSettingsFragment.java#L75

这篇关于如何获取当前访问令牌在Facebook的Andr​​oid SDK中V4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:31