MrCooL
MrCooL

Reputation: 926

How to have a notification of expired Facebook user access token

I've seen a lot of examples, and read some articles to know well about the access token expiration and it was explained pretty well in this article, Access Token Expiration.

I know how to renew the expiration access token. However, the problem is that, as explained in the article, there are possibly 4 reasons for expiration of access token .

Hence, there is no certainty when the access token will be expired and it can be at anytime. The conventional solution is by using offline_permision which will be deprecated by FB soon.

Here is what I'm trying to achieve:

  1. Once the access token expired, and while when user is using our facebook app, I want FB to notifies me.

  2. Once receiving notification, renew the access token.

I don't think the following achieves what I want.

FB.Event.subscribe('auth.authResponseChange', function(response) {
     alert('The status of the session is: ' + response.status);
});

Hence, is there any way to have notification from FB when the access token is expired ?

Upvotes: 3

Views: 1022

Answers (1)

LocalPCGuy
LocalPCGuy

Reputation: 6116

When you try to access the API with an expired token, Facebook will send back an error message. You just check for that error in your callback function that handles the Facebook response, and if an error exists, refresh the token and try the API call again.

From Facebook:

Facebook doesn't notify you that a previously issued access token has become invalid. Unless you have persisted the expiry time passed to your application along with the access token, often the first your application knows that a given token has become invalid is when you attempt to make a request to the API.

Upvotes: 2

Related Questions