What is the correct way to refresh Facebook OAuth2 access token after it expires?

FacebookFacebook Graph-ApiOauth 2.0

Facebook Problem Overview


As I understand it, this is the basic process for new Facebook iframe canvas apps using the OAuth2 API in a nutshell:

  1. Redirect to (or have user click link to) app's authorization URL
  2. User authorizes and is redirected to your callback URL
  3. Callback uses "code" parameter to get a access token
  4. Access token is used with Graph API to pull or push information

The problem is that access tokens expire relatively quickly and need to be "refreshed", so my questions are 1) how do you detect that the token has expired aside from trying to use it and simply getting an error? and 2) what is the best practice for obtaining a new token?

Currently, I just detect that there was an error trying to get the user's information with their access token, then redirect to the authorization URL again -- since they already authorized the app a blank page flashes by and they are redirected back to my app callback where I get a fresh token. It's so clunky I can't believe this is the proper method.

Facebook Solutions


Solution 1 - Facebook

  1. The only way to tell if a cookie is valid is to use it and catch the error if it is expired. There is no polling method or anything to check if a token is valid.

  2. To get a new token, simply redirect the user to the authentication page again. Because they have already authorized your app they will instantly be redirected back to your app and you will have a new token. They won't be prompted to allow since they have already done that.

In short, there are no tricks to this. You are already doing it correctly.

Solution 2 - Facebook

Recently, facebook has made some changes to access tokens which allows them to be refreshed periodically.

https://graph.facebook.com/oauth/access_token?
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN 

For more details, check here: https://developers.facebook.com/docs/roadmap/completed-changes/offline-access-removal

Solution 3 - Facebook

//you just need more step because the access token you are getting will expire in 1 hour
    //you can overcome this in step 5

    1-Redirect to (or have user click link to) app's authorization URL
2-User authorizes and is redirected to your callback URL
3-Callback uses "code" parameter to get a access token
4-Access token is used with Graph API to pull or push information
    5-exchange short-lived access token you just got with 60 day access token
    https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=EXISTING_ACCESS_TOKEN
    6-after 60 day the user must login again to your app and the steps from 1-5 will be repeated.
    --the real problem you will face is how to make the user visit your app page again

Solution 4 - Facebook

Facebook has removed the feature of refresh the access token on the "behalf of" mode. The best and easy way is to redirect the user to facebook login page to re-oauth the app. Find https://developers.facebook.com/docs/facebook-login/access-tokens#extending" title="extending graph api access-tokens">facbook doc here

Solution 5 - Facebook

if user has already authorized your application and access token expired. you can redirect user to authentication page again. but oauth dialog doestn't show because user already authorized your application. he will redirect to redirect_url parameter you used.

Solution 6 - Facebook

{ "error": { "message": "Missing redirect_uri parameter.", "type": "OAuthException", "code": 191, "fbtrace_id": "BHvng7s53ra" }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmtjhaxView Question on Stackoverflow
Solution 1 - FacebookNate TottenView Answer on Stackoverflow
Solution 2 - FacebookloganView Answer on Stackoverflow
Solution 3 - Facebookashraf mohammedView Answer on Stackoverflow
Solution 4 - FacebookNarendra SinghView Answer on Stackoverflow
Solution 5 - FacebookmurnaxView Answer on Stackoverflow
Solution 6 - FacebookVishnu LvzView Answer on Stackoverflow