Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

OAuthException - Code was invalid or expired. #31

Closed
CFPro opened this issue Dec 16, 2011 · 5 comments
Closed

OAuthException - Code was invalid or expired. #31

CFPro opened this issue Dec 16, 2011 · 5 comments

Comments

@CFPro
Copy link

CFPro commented Dec 16, 2011

My token keeps expiring, and I get "OAuthException - Code was invalid or expired."
I found out their are 4 scenarios which cause this condition:

http://developers.facebook.com/blog/post/500/

How can I error check and reacquire a valid token?

TIA

@CFPro
Copy link
Author

CFPro commented Dec 16, 2011

Upon closer inspection, I noticed the following in /samples/website/index.cfm:

if (userId) {
    userAccessToken = facebookApp.getUserAccessToken();
    try {
        facebookGraphAPI = new FacebookGraphAPI(accessToken=userAccessToken, appId=APP_ID);
        userObject = facebookGraphAPI.getObject(id=userId);
        userFriends = facebookGraphAPI.getConnections(id=userId, type='friends', limit=10);
        authenticated = true;
    } catch (any exception) {
        // Usually an invalid session (OAuthInvalidTokenException), for example if the user logged out from facebook.com
        userId = 0;
        facebookGraphAPI = new FacebookGraphAPI();
    }
} else {
    facebookGraphAPI = new FacebookGraphAPI();
}

if I put

userAccessToken = facebookApp.getUserAccessToken();

inside the try condition, then it seems to resolve the error... like so

if (userId) {
    try {
        userAccessToken = facebookApp.getUserAccessToken();
        facebookGraphAPI = new FacebookGraphAPI(accessToken=userAccessToken, appId=APP_ID);
        userObject = facebookGraphAPI.getObject(id=userId);
        userFriends = facebookGraphAPI.getConnections(id=userId, type='friends', limit=10);
        authenticated = true;
    } catch (any exception) {
        // Usually an invalid session (OAuthInvalidTokenException), for example if the user logged out from facebook.com
        userId = 0;
        facebookGraphAPI = new FacebookGraphAPI();
    }
} else {
    facebookGraphAPI = new FacebookGraphAPI();
}

This might not be the best solution, but it seems to work.

@benorama
Copy link
Contributor

benorama commented Jan 2, 2012

Indeed, the user access token call should be inside the try/catch.

@benorama benorama closed this as completed Jan 2, 2012
@jricheyicnd
Copy link

I have the same issue. We are building a site for a customer where we are displaying their FB albums, events and status updates on the site. So the user is always the same, there is no FB user login. But the access token for our user keeps expiring. So looking at the code it says:

// See if there is a user from a cookie or session
userId = facebookApp.getUserId();

So my user is not from a cookie or session, its a persistent user so how can I request the access token for our user - we know the username and password but I just don't know how to request a new access token when it expires. Right now we have to manually log into FB and request a new access token by hand but that won't work for a production site.

@benorama
Copy link
Contributor

By default, access token expires after 2 hours.
Are you using the latest Facebook SDK?
We have recently added some code to automatically exchange token before expiration.

You can also manually get an extended token, valid during 60 days :

var result = facebookApp.exchangeAccessToken(currentToken); // Current token must be valid
writeDump(result); // Struct with 'access_token' and 'expiration_time'

You might store this token in DB with the new expiration time and check it each time user log in.

@joejoecc
Copy link

joejoecc commented Aug 1, 2013

Implementing @CFPro suggestion of putting

userAccessToken = facebookApp.getUserAccessToken();

inside the try condition, resolved my error with the default set-up. If I just installed the default example after 10 minutes of inactivity I would get the Oath 100 error ... but this seems to solve it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants