如何获得对应用程序当前Facebook的访问令牌启动? [英] How to get current Facebook access token on app start?

查看:185
本文介绍了如何获得对应用程序当前Facebook的访问令牌启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序启动我需要知道,如果用户登录或不显示登录页面或没有。

我的第一次尝试是要求 AccessToken.getCurrentAccessToken(),但这不起作用,见<一href=\"http://stackoverflow.com/a/29854249/1016472\">http://stackoverflow.com/a/29854249/1016472

我的第二次​​尝试是使用AccessTokenTracker但这并不太多工作。要使用AccessTokenTracker我必须首先初始化的Facebook SDK。但这个初始化期间访问令牌发送到跟踪器。跟踪器是不是在这个时候创建​​的,无法接收令牌。

我的第三次尝试是创建我自己的AccessTokenTracker它看起来像原来的。我创建并注册它之前,我初始化SDK。现在我的Tracker的GET就是在这个SDK初始化过程的标记。

通过这方面的知识我在打开Facebook的一个错误报告与问题:

从Facebook答案是:


  

这看起来并不像臭虫。该AccessTokenTracker
  :: onCurrentAccessTokenChanged应该让你知道什么时候班里有
  收到的令牌。


我知道,这是行不通的。现在我知道有没有错误。

但如何解决我的问题?

这不工作:

FacebookSdk.sdkInitialize(getApplicationContext());
mAccessTokenTracker =新AccessTokenTracker(){    @覆盖
    保护无效onCurrentAccessTokenChanged(的accessToken oldAccessToken,的accessToken currentAccessToken){        Timber.i(AccessTokenTracker oldAccessToken:+ oldAccessToken + - currentAccessToken:+ currentAccessToken);
    }
};


解决方案

您需要像你说的,首先初始化SDK。然后创建访问令牌跟踪器,然后通过AccessToken.getCurrentAccessToken()检查当前访问令牌。

如果该SDK初始化是足够快的标记设置,然后才能创建跟踪器,那么你的currentAccessToken的通话将获得令牌。如果SDK初始化比则跟踪登记慢那么跟踪器将获得访问令牌。

On app start I need to know if the user is logged in or not to show a login page or not.

My first try was to call AccessToken.getCurrentAccessToken(), but this doesn't work, see http://stackoverflow.com/a/29854249/1016472

My 2nd try was to use the AccessTokenTracker but this doesn't work too. To use the AccessTokenTracker I have to initialize the Facebook SDK first. But during this initialization the access token is send to the tracker. The tracker isn't created at this time and can't receive the token.

My 3rd try was to create my own AccessTokenTracker which looks like the original one. I created and registered it before I initialize the SDK. And now my tracker get's the token during this SDK initialization process.

With this knowledge I open a bug report at Facebook with the questions:

  • Why the AccessTokenTracker depends on the initialized SDK. I think this is a mistake.
  • What is the right way to see if a user is logged in or not on app start.

The answer from Facebook:

This doesn't look like bug. The AccessTokenTracker ::onCurrentAccessTokenChanged should let you know when the class has received a token.

I know that this is not working. And now I know there is no bug.

But how to solve my problem?

This doesn't work:

FacebookSdk.sdkInitialize(getApplicationContext());
mAccessTokenTracker = new AccessTokenTracker() {

    @Override
    protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

        Timber.i("AccessTokenTracker oldAccessToken: " + oldAccessToken + " - currentAccessToken: " + currentAccessToken);
    }
};

解决方案

You need to initialize the SDK first as you said. Then create the access token tracker, and then check the current access token via AccessToken.getCurrentAccessToken().

If the SDK initializer is fast enough that the token is set before you can create the tracker, then your call of currentAccessToken will get the token. If the sdk initialization is slower than then tracker registration then the tracker will get the access token.

这篇关于如何获得对应用程序当前Facebook的访问令牌启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆