Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook: Refreshing long-lived access token automatically

I'm storing long-lived access tokens for users of my application that have associated their Facebook accounts to it. Since the demise of the offline_access tokens, these long-lived tokens have an expiry date of "about 60 days." However, they can refresh themselves when the user interacts with Facebook. According to the documentation:

These tokens will be refreshed once per day when the person using your app makes a request to Facebook's servers. If no requests are made, the token will expire after about 60 days and the person will have to go through the login flow again to get a new token.

What I'd like to know is what constitutes making a request to Facebook's servers. Does the user have to log in to the Facebook website, mobile app, or use a Like button somewhere? Or does my application making a request on behalf of the user count as well?

Also, when the tokens are refreshed, are they refreshed for another 60 days? Or are they refreshed for a smaller duration?

I wasn't able to find these specific answers in the documentation or in other questions asked here, so thanks in advance to anyone who might have more details.

like image 810
JSTL Avatar asked Jul 15 '14 19:07

JSTL


People also ask

Does Facebook use refresh token?

Facebook does not provide a refresh token. Facebook provides two kinds of access tokens, Short lived access token: A token that is expired after a short period of time (about 2 hours). Short lived access tokens are usually used on web clients.

Why do I have access and refresh tokens?

Refresh Tokens are useful because they allow applications to get new Access Tokens without forcing users to login to the system repeatedly. Typically our Access Tokens last for 20 minutes.

How do I get Facebook access token that never expires?

In the Access Token Debugger that will open up, click on the 'Extend Access Token' button at the bottom of the page. A new access token should be displayed and the text above it should say that it never expires.

How do you refresh a long live token?

Refresh a Long-Lived TokenUse the GET /refresh_access_token endpoint to refresh unexpired long-lived Instagram User Access tokens. Refreshing a long-lived token makes it valid for 60 days again. Long-lived tokens that have not been refreshed in 60 days will expire.


2 Answers

Every time you use Facebook SDK so it makes any Graph API call, tokens will be refreshed. You can see this in their source code, in AccessTokenManager there is function extendAccessTokenIfNeeded(), and that function is called inside GraphRequest in function executeConnectionAndWait().

You can also manually refresh tokens by calling:

AccessToken.refreshCurrentAccessTokenAsync();

I found one exception to this. Only sso tokens can be refreshed, which means if user logged in to your app via facebook app. If user logged in via browser, token will remain the same.

like image 110
MarkoR Avatar answered Sep 21 '22 19:09

MarkoR


The previous line to the one you pasted is important: Native mobile applications using Facebook's SDKs will get long-lived access tokens, good for about 60 days

The section you pulled out refers only to iOS and Android apps using the Facebook SDK - the SDK makes an API call to extend the token, which will only work from the SDK and for tokens produced by the native mobile SDKs-

Other apps (e.g websites, apps on facebook.com) need to use the login flows documented elsewhere in the documentation and require the user to be logged into Facebook in their browser

like image 23
Igy Avatar answered Sep 18 '22 19:09

Igy