7
votes

Background:

I know that this question has been asked before, I have gone through all answers I have found, but it seems that no one have given a satisfying answer to it yet. I have seen that Facebook had a bug open about this but closed it due to lack of info. Thats why I ask again to see if someone out there now have found any good solution.

I'm trying to get a short-lived access token to be able to get a users profile. This worked fine up until some time ago and now I only get "OAuthException: This authorization code has been used." back from the api call.

Technologies used:

  • PHP (no third part libraries or Facebook SDKs for Facebook API interaction)
  • cURL
  • Graph API
  • https

Logical Flow

  1. Get the url for the login dialogue looking similar to this:

    https://graph.facebook.com/oauth/authorize?client_id=[APP_ID]&redirect_uri=[URLENCODED_REDIRECT_URI]&display=popup&scope=publish_stream

  2. Login through the Facebook login dialogue. On success redirect the user to the specified callback URI and add the "code" quersy-string variable.

  3. When the user is redirected to the specified callback URI it immediately try to get get a short-lived accesstoken with the "code" variable provided by the Facebook login dialogue. I do that by calling this url:

    https://graph.facebook.com/oauth/access_token?client_id=[APP_ID]&redirect_uri=[URLENCODED_REDIRECT_URI]&client_secret=[APP_SECRET]&code=[CODE_SENT_FROM_FACEBOOK]

I receive the following response EVERY TIME: {"error":{"message":"This authorization code has expired.","type":"OAuthException","code":100}}

I already know that:

  1. I shall get a short-lived access token
  2. the code is only valid for 10 minutes
  3. I can only use the code once (my logging confirm that I only call the access_token request just once)

What have I missed? Why has it suddenly stopped working? Any answer, tip or trick is very appreciated! Thanks in advance.

1
Have you tried exchanging the short-lived token for the long-lived token and use that instead? It should just be one extra step to insert into your code. - Shazbot
you're sure you don't have a redirect loop somewhere? That error comes when you've already used the code to get an access token or when the code has expired...Post some of your PHP maybe. - tattvamasi
I am having this issue too, but am calling $session = $helper->getSession(); everytime the user refreshes the page or visits a new page. Have not found out how to fix it yet either :( - jordan

1 Answers

-1
votes

If I understood.

this token change everytime when you log in on facebook.

You need to get a new token always you make a new login.