您可以通過異常處理來完成。如果您的訪問令牌已過期或無效,則會向您發送錯誤消息並向您提供下面提到的消息。
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;
}
我上述兩種功能做出。所以,只要你需要使用訪問令牌,只需調用擴展訪問令牌並將其保存到你的數據庫。所以你的訪問令牌永遠不會過期。
非常感謝,這是可以理解的。所以如果我發現了一些錯誤,我必須得到新的訪問令牌。如果我想在牆上張貼東西,我需要訪問令牌,那麼我必須將令牌保存在我的數據庫中,是的,或者我應該始終在我的數據庫中保留訪問令牌? – robinloop 2013-03-16 11:42:07
是的,如果你發現一些錯誤比你需要獲得新的訪問令牌。並把它保存到你的數據庫只需替換舊的。多數民衆贊成它..我已經完成了上述相同的方式.. – 2013-03-16 11:44:43
它更好地保持60天令牌在我的分貝,對不對?所以當用戶登錄時,立即獲得新的60天令牌? – robinloop 2013-03-16 11:52:29