Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if Facebook access token is still valid?

My site utilizes lifetime access tokens (offline_access). However, if the user changes his/her password, the access token gets reset. Is there a method to check if the current access token is valid before making calls to the Graph API? Thanks for your time.

like image 291
axsuul Avatar asked Oct 02 '10 09:10

axsuul


People also ask

How do I know if my access token is expired?

This can be done using the following steps: convert expires_in to an expire time (epoch, RFC-3339/ISO-8601 datetime, etc.) store the expire time. on each resource request, check the current time against the expire time and make a token refresh request before the resource request if the access_token has expired.

Do Facebook tokens expire?

Authentication Expiration If your app uses one of the Facebook SDKs, this token lasts for about 60 days. However, the SDKs automatically refresh the token whenever the person uses your app, so the tokens expire 60 days after last use.

Does access token expire?

By default, access tokens are valid for 60 days and programmatic refresh tokens are valid for a year. The member must reauthorize your application when refresh tokens expire.

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.


8 Answers

Offline, without sending anything to facebook - I don't think so. The easiest way is probably to send a request to:

https://graph.facebook.com/me?access_token=...

Facebook also supports subscriptions for real-time updates, but I am not sure how to apply them to this situation.

like image 75
serg Avatar answered Oct 02 '22 07:10

serg


If you want to know the token expiry time you can pass a open graph url using appid and token as below it will work .

https://graph.facebook.com/oauth/access_token_info?client_id=APPID&access_token=xxxxxxxxx
like image 26
Chaitanya Bharat Avatar answered Oct 02 '22 08:10

Chaitanya Bharat


Basically, FB wants you to poll for it, or to detect the case and redirect the user to get a reauth to occur. Annoying, but official:

(Old, out of date link. See below) https://developers.facebook.com/blog/post/500/

Edit: Facebook changed their link structure without redirects. Not surprised.

https://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/

like image 23
Otto Avatar answered Oct 02 '22 08:10

Otto


You can check the token using the token debug service , take a look here

https://graph.facebook.com/debug_token?input_token=INPUT_TOKEN&access_token=ACCESS_TOKEN

https://developers.facebook.com/docs/howtos/login/debugging-access-tokens/

like image 31
Avi Kapuya Avatar answered Oct 02 '22 07:10

Avi Kapuya


The real time updates would allow you to solve this problem, but it would be pretty complicated. Basically, you can subscribe to updates that will tell you 1) if the user removed the app or 2) if the user removed permissions. You could use this to store the current permissions of the faceboook user. This way, if the user removed your app you would know that the access token is expired.

Real time updates is actually facebooks recommended way of handling permissions. Many apps make api calls every time a page is loaded to check for permissions. This tends to be slow and unreliable.

like image 39
Nathan Totten Avatar answered Oct 02 '22 09:10

Nathan Totten


I went through these posts, bud I found very good solutions like this:

GET graph.facebook.com/debug_token?
    input_token={token-to-inspect}
    &access_token={app_id}|{app_secret}

Response from this request provides you everything you need:

  • your app ID - this verifies that token is from your application
  • application name - which can be also checked
  • expires_at - token expiration time
  • is_valid - boolean for check up
  • user_id - which you can also compare and check

Just note that "|" sign must be there as a letter

like image 25
Pavol Golias Avatar answered Oct 02 '22 07:10

Pavol Golias


        //When user access token expires user must be logged in and renew the access token him self.it is a Facebook policy 
        //you can overcome this by sending email to users who have expired access token.
        //create a table of successful sending to monitor sending process
        //if any failure happened with the user an email is sent to him to ask him to activate there account again.with a link to your subscription page.
        //and here is the code should be written on that page. 
         $app_id = "YOUR_APP_ID";
         $app_secret = "YOUR_APP_SECRET"; 
         $my_url = "YOUR_POST_LOGIN_URL";

        // known valid access token stored in a database 
        $access_token = "YOUR_STORED_ACCESS_TOKEN";

        $code = $_REQUEST["code"];

       // If we get a code, it means that we have re-authed the user 
       //and can get a valid access_token. 
       if (isset($code)) {
         $token_url="https://graph.facebook.com/oauth/access_token?client_id="
           . $app_id . "&redirect_uri=" . urlencode($my_url) 
           . "&client_secret=" . $app_secret 
           . "&code=" . $code . "&display=popup";
         $response = file_get_contents($token_url);
         $params = null;
         parse_str($response, $params);
         $access_token = $params['access_token'];
       }


       // Attempt to query the graph:
       $graph_url = "https://graph.facebook.com/me?"
         . "access_token=" . $access_token;
       $response = curl_get_file_contents($graph_url);
       $decoded_response = json_decode($response);

       //Check for errors 
       if ($decoded_response->error) {
       // check to see if this is an oAuth error:
         if ($decoded_response->error->type== "OAuthException") {
           // Retrieving a valid access token. 
           $dialog_url= "https://www.facebook.com/dialog/oauth?"
             . "client_id=" . $app_id 
             . "&redirect_uri=" . urlencode($my_url);
           echo("<script> top.location.href='" . $dialog_url 
          . "'</script>");
        }
        else {
          echo "other error has happened";
        }
      } 
      else {
      // success
        echo("success" . $decoded_response->name);
        echo($access_token);
      }

      // note this wrapper function exists in order to circumvent PHP's 
      //strict obeying of HTTP error codes.  In this case, Facebook 
      //returns error code 400 which PHP obeys and wipes out 
      //the response.
      function curl_get_file_contents($URL) {
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
        curl_close($c);
        if ($contents) return $contents;
        else return FALSE;
      }
like image 36
ashraf mohammed Avatar answered Oct 02 '22 07:10

ashraf mohammed


Offline - it is not possible

Ask that user has given permission or not:

https://graph.facebook.com/{facebook-id}/permissions?access_token={access-token}

If access token is invalid then it will give error:

{  
   error:{  
      message:"The access token could not be decrypted",
      type:"OAuthException",
      code:190
   }
}

Otherwise it will give list of permission that user has given:

data:[  
   {  
      installed:1,
      ...... permission list......... 
      bookmarked:1
   }
]
like image 28
Hitesh Modha Avatar answered Oct 02 '22 07:10

Hitesh Modha