所以如果我想捕获过期访问令牌的错误,我必须检查出来https://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/好的,但是这有点不清楚。
// 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)) {
所以如果我得到$_REQUEST["code"]
就意味着用户有访问令牌?
我的应用打印用户喜欢,如果例如用户更改密码,我必须获得新的访问令牌。我应该如何使这些令牌有效?在这种情况下,facebook文档对我来说真的很不清楚;/
可以通过异常处理来实现。如果您的访问令牌过期或无效,它将向您抛出错误并给您下面提到的消息。
public function getUserInfo($access_token)
{
try {
$user_info = $this->facebook->api(
'/me',
'GET',
array('access_token' =>$access_token)
);
}
catch(Exception $e){
$message = $e->getMessage();
if ((strpos($message, 'Error validating access token') !== false) ||
(strpos($message, 'Invalid OAuth access token') !== false) ||
(strpos($message, 'An active access token must be used') !== false)
) {
echo 'Your custom Message';
}
}
}
你可以把你的访问令牌传递给你的函数,然后像上面的异常处理一样检查。
希望对你有帮助。
通过这种方式,您可以获得扩展访问令牌
/**
* Getting User Acess Tocken , extended it and save it in to database......
*/
public function getAccessToken($user_id,$fb_account_id)
{
$access_token=$this->facebook->getAccessToken();
$extended_access_token=$this->getExtendedAccessToken($access_token);
/**
* To save Access tocken and other necessary option
*/
$usr_bus_model = new Application_Model_UserBusinessAccounts;
$usr_bus_model->loadAccount($user_id,'Facebook',(int)$fb_account_id,false);
$usr_bus_model->details=$extended_access_token;
if(!empty($extended_access_token))
{
$usr_bus_model->status= 'active';
}
$usr_bus_model->save();
return $extended_access_token;
}
public function getExtendedAccessToken($access_token)
{
$token_url="https://graph.facebook.com/oauth/access_token";
$params=array('client_id'=>self :: appId,'client_secret'=>self :: appSecretId,'grant_type'=>'fb_exchange_token','fb_exchange_token'=>$access_token);
$response = $this->curl($token_url,$params);
$response = explode ('=',$response);
$response = explode ('&',$response[1]);
$response = $response[0];
return $response;
}
我已经做了上面两个函数。因此,当您需要使用访问令牌时,只需调用扩展访问令牌并将其保存到数据库中。所以你的访问令牌永远不会过期