Facebook:在 Android 刷新 AccessToken

Facebook: refresh AccessToken on Android

他们在 AccessToken docu 中说了以下内容:"Native mobile applications using Facebook's SDKs will get long-lived access tokens, good for about 60 days. These tokens will be refreshed once per day when the person using your app makes a request to Facebook's servers."
这是否意味着只有当对 Facebook 服务器的请求是从客户端 Android 应用程序发出的(我不确定如何解释“......使用你的应用程序的人......”)时,访问令牌才会是刷新了?
在我的应用程序中,我使用 AccessToken.getCurrentAccessToken() 检索访问令牌,将访问令牌发送到我的服务器,服务器向 Facebook 的服务器发出请求,目的是验证接收到的访问令牌。
在那种情况下访问令牌也会被刷新吗? (在这种情况下,服务器正在向 FB 的服务器发出请求,而不是直接 "the person using my app")

谢谢

编辑:这可能会帮助其他人节省时间: 我发现访问令牌在某些情况下会在初始化 FacebookSDK 时自动刷新(Facebook 文档没有清楚地解释访问令牌是如何刷新的 - 请参阅我上面的问题)。
请注意我正在为 Android.
使用 FacebookSDK 4.0 按照以下步骤操作:

  1. FacebookSDK.sdkInitialize(Context, InitializeCallback)

    if (AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile() == null)

  2. Profile.fetchProfileForCurrentAccessToken()

  3. Utility.getGraphMeRequestWithCacheAsync()

    GraphRequest graphRequest = getGraphMeRequestWithCache(accessToken); graphRequest.setCallback(graphCallback); graphRequest.executeAsync();

  4. ...executeBatchAsync(GraphRequestBatch requests)
    GraphRequestAsyncTask asyncTask = new GraphRequestAsyncTask(requests); asyncTask.executeOnSettingsExecutor();
  5. 参见GraphRequestAsyncTask doInBackground()方法

if (connection == null) { return requests.executeAndWait(); } else { return GraphRequest.executeConnectionAndWait(connection, requests); }

对于这两种情况,代码将以这种方法结束:

public static List<GraphResponse> executeConnectionAndWait(
            HttpURLConnection connection,
            GraphRequestBatch requests)
  1. 注意 AccessTokenManager.getInstance().extendAccessTokenIfNeeded(); 行。 顾名思义,它每天最多会扩展您的访问令牌一次。

您可以使用refreshCurrentAccessTokenAsync方法:

AccessToken.refreshCurrentAccessTokenAsync();

文档说明(强调我的):

When you use the iOS, Android, or JavaScript SDK, the SDK will automatically refresh tokens if the person has used your app within the last 90 days. Native mobile apps using Facebook's SDKs get long-lived User access tokens, good for about 60 days. These tokens are refreshed once per day, when the person using your app makes a request to Facebook's servers.

这个的意思是,如果你使用官方SDK built-in Graph API客户端(GraphRequest and friends), then after each request the SDK will attempt to refresh the token(但不会比一天一次更频繁)。

为此,SDK 使用未记录的图表 API:

https://graph.facebook.com/v3.2/oauth/access_token?access_token={user-access-token}&grant_type=fb_extend_sso_token&client_id={your-app_id}

(它可能在所有 Graph API 版本中可用,但我没有测试过)

Android SDK 还直接在 public API 和 refreshCurrentAccessTokenAsync 方法中公开了此令牌刷新功能。

请务必注意,令牌刷新适用于所谓的“SSO”令牌,即从 Facebook 应用程序收到的令牌。如果用户没有在他们的设备上安装 Facebook 应用程序,或者出于任何其他原因 log-in 流程使用了 webview 变体,那么以这种方式收到的令牌 将永远不会被刷新.据我所知,这在任何地方都没有记录。

上面提到的未记录的 API returns 当您尝试刷新从 webview 流收到的令牌时出现 400 错误:

HTTP/1.1 400 Bad Request
...
{"error":{"message":"(#10) The access token was not obtained using single sign-on","type":"OAuthException","code":10,"fbtrace_id":"AbGFcwx4aOhY0sgEkLdAR34"}}

整个答案是在考虑 Android 的情况下编写的,但我认为它也适用于 iOS SDK(但我还没有测试过)。