user2302780
user2302780

Reputation: 1249

how to resolve facebook acces token expiration issue?

I have created a facebook place serach with graph api. But the access key expires every two hours. For this I have implemented the below code

 $app_id = "---";
 $app_secret = "----"; 
 $my_url = "";
$code = $_REQUEST["code"]; 
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'];
}
$FacebookGraphURL = 'https://graph.facebook.com/search?fields=id,username,name,category,website,likes,location&q='.$keyword.'&type=place&center='.$center.'&distance='.$radius.'&limit=5&access_token='.$access_token;
    $FacebookGraphJSON = file_get_contents($FacebookGraphURL);

But this is giving the below error :

Notice: Undefined index: code in /opt/lampp/htdocs/APIcomparison/facebook_graph.php on line 24

what I am missing ? another thing I don't know what to use $my_url

Thanks is advance.

Upvotes: 0

Views: 916

Answers (1)

Cormac Driver
Cormac Driver

Reputation: 2521

Are you generating the access token via the Graph Explorer tool? If so, those tokens have a 2-hour expiry time.

You can generate an access token with a 60-day expiry time by going through the Facebook Log In / OAuth process, during which you grant your application access to your (or any user's) Facebook user account by clicking the 'allow' button on the authorization dialog. Once you've obtained an access token you can exchange it for a long-lived token.

Details on Facebook OAuth here: https://developers.facebook.com/docs/reference/dialogs/oauth/

A simple way to generate an access token is to use the Facebook OAuth support provided by Temboo. If you just wanted a single token then you can use the Facebook OAuth wizard, meaning that you won't have to write any code to generate the token. Details here: https://www.temboo.com/library/Library/Facebook/

(Full disclosure: I work at Temboo)

Upvotes: 1

Related Questions