Skip to main content

Do Facebook Oauth 2.0 Access Tokens Expire?


I am playing around with the Oauth 2.0 authorization in Facebook and was wondering if the access tokens Facebook passes out ever expire. If so, is there a way to request a long-life access token?



Source: Tips4allCCNA FINAL EXAM

Comments

  1. After digging around a bit, i found this. It seems to be the answer:

    offline_access:
    Enables your application to perform authorized requests on behalf of the user at any time. By default, most access tokens expire after a short time period to ensure applications only make requests on behalf of the user when the are actively using the application. This permission makes the access token returned by our OAuth endpoint long-lived.

    Its a permission value requested.

    http://developers.facebook.com/docs/authentication/permissions

    ReplyDelete
  2. Try this may be it will help full for you

    https://graph.facebook.com/oauth/authorize?
    client_id=127605460617602&
    scope=offline_access,read_stream,user_photos,user_videos,publish_stream&
    redirect_uri=http://www.example.com/


    To get lifetime Access Token you have to use scope=offline_access

    Meaning of scope=offline_access is that :-


    Enables your application to perform authorized requests on behalf of
    the user at any time. By default, most access tokens expire after a
    short time period to ensure applications only make requests on behalf
    of the user when the are actively using the application. This
    permission makes the access token returned by our OAuth endpoint
    long-lived.

    ReplyDelete
  3. I came here with the same question as the OP, but the answers suggesting the use of offline_access are raising red flags for me.

    Security-wise, getting offline access to a user's Facebook account is qualitatively different and far more powerful than just using Facebook for single sign on, and should not be used lightly (unless you really need it). When a user grants this permission, "the application" can examine the user's account from anywhere at any time. I put "the application" in quotes because it's actually any tool that has the credentials -- you could script up a whole suite of tools that have nothing to do with the web server that can access whatever info the user has agreed to share to those credentials.

    I would not use this feature to work around a short token lifetime; that's not its intended purpose. Indeed, token lifetime itself is a security feature. I'm still looking for details about the proper usage of these tokens (Can I persist them? How do/should I secure them? Does Facebook embed the OAuth 2.0 "refresh token" inside the main one? If not, where is it and/or how do I refresh?), but I'm pretty sure offline_access isn't the right way.

    ReplyDelete
  4. Note that Facebook is now deprecating the offline_access permission in favor of tokens for which you can request an "upgrade" to the expiry. I'm just now dealing with this, myself, so I don't have much more to say, but this doc may help:

    https://developers.facebook.com/docs/offline-access-deprecation/

    ReplyDelete
  5. Yes, they do expire. There is an 'expires' value that is passed along with the 'access_token', and from what I can tell it's about 2 hours. I've been searching, but I don't see a way to request a longer expiration time.

    ReplyDelete
  6. since i had the same problem - see the excellent post on this topic from ben biddington, who clarified all this issues with the wrong token and the right type to send for the requests.

    http://benbiddington.wordpress.com/2010/04/23/facebook-graph-api-getting-access-tokens/

    ReplyDelete
  7. You can always refresh the user's access token every time the user logs into your site through facebook.
    The offline access can't guarantee you get a life-long time access token, the access token changes whenever the user revoke you application access or the user changes his/her password.

    Quoted from facebook http://developers.facebook.com/docs/authentication/


    Note: If the application has not requested offline_access permission, the access token is time-bounded. Time-bounded access token also get invalidated when the user logs out of Facebook. If the application has obtained offline_access permission from the user, the access token does not have an expiry. However it gets invalidated whenever the user changes his/her password.


    Assume you store the user's facebook uid and access token in a users table in your database,every time the user clicks on the "Login with facebook" button, you check the login statususing facebook Javascript API, and then examine the connection status from the response,if the user has connected to your site, you can then update the access token in the table.

    ReplyDelete
  8. log into facebook account and edit your application settings(account -> application setting ->additional permission of the application which use your account). uncheck the permission (Access my data when I'm not using the application(offline_access)). Then face will book issue a new token when you log in to the application.

    ReplyDelete
  9. check the following things when you interact with facebook graph api.

    1) Application connect URL should be the base of your "redirect_uri"
    connect URL:- www.x-minds.org/fb/connect/
    redirect_uri - www.x-minds.org/fb/connect/redirect
    2) Your "redirect_uri" should be same in the both case (when you request for a verification code and request for an access_token)
    redirect_uri - www.x-minds.org/fb/connect/redirect
    3) you should encode the the argument when you request for an access_token
    4) shouldn't pass the argument (type=client_cred) when you request for an access_token. the authorization server will issue a token without session part. we can't use this token with "me" alias in graph api. This token will have length of (40) but a token with session part will have a length of(81).
    An access token without session part will work with some cases

    eg: -https://graph.facebook.com/?access_token=116122545078207|EyWJJYqrdgQgV1bfueck320z7MM.
    But Graph API with "me" alias will work with only token with session part.

    ReplyDelete
  10. I don't know when exactly the tokens expire, but they do, otherwise there wouldn't be an option to give offline permissions.

    Anyway, sometimes requiring the user to give offline permissions is an overkill. Depending on your needs, maybe it's enough that the token remains valid as long as the website is opened in the user's browser. For this there may be a simpler solution - relogging the user in periodically using an iframe: facebook auto re-login from cookie php

    Worked for me...

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex