Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook: refresh AccessToken on Android

They are saying in the AccessToken docu the following: "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."
Does it mean that only if the request to Facebook's servers is made from client Android app (I am not sure how to interpret the "... the person using your app ...") then the access token will be refreshed ?
In my app, I retrieve the access token using AccessToken.getCurrentAccessToken(), send the access token to my server and the server makes the request to Facebook's servers with the aim to validate the received access token.
Will the access token be refreshed also in that case ? (in that case the server is doing the request to FB's servers and not directly "the person using my app")

  • If yes, How does the Facebook SDK in the Android client app knows that my server made the request to Facebook's servers ? Does theAccessToken.getCurrentAccessToken() in Android client returns the refreshed access token ?

Thank you

EDIT: This may help the others to save time: I found out that access token is under certain conditions automatically refreshed at initializing the FacebookSDK (Facebook docu doesn't clearly explain how the access token is refreshed - See my question above).
Please notice I am using FacebookSDK 4.0 for Android.
Follow the steps bellow:

  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. See GraphRequestAsyncTask doInBackground() method

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

For both cases the code will end up in this method:

public static List<GraphResponse> executeConnectionAndWait(
            HttpURLConnection connection,
            GraphRequestBatch requests)
  1. Notice AccessTokenManager.getInstance().extendAccessTokenIfNeeded(); line. Which as the name suggest will extend your access token at most once per day.
like image 905
FilipR Avatar asked Jul 12 '15 16:07

FilipR


1 Answers

You can use the refreshCurrentAccessTokenAsync method:

AccessToken.refreshCurrentAccessTokenAsync();
like image 150
Pankaj Arora Avatar answered Oct 31 '22 00:10

Pankaj Arora