Php 我的数据库中的访问令牌
所以,如果我想用过期的访问令牌捕获错误,我必须检查这个 好吧,但有点不清楚Php 我的数据库中的访问令牌,php,facebook,access-token,Php,Facebook,Access Token,所以,如果我想用过期的访问令牌捕获错误,我必须检查这个 好吧,但有点不清楚 // 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
// 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)) {
所以,如果我得到$\u请求[“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;
}
我做了以上两个函数。因此,每当您需要使用访问令牌时,也只需调用扩展访问令牌并将其保存到数据库中。因此,您的访问令牌将永远不会过期。非常感谢,这是可以理解的。所以,如果我发现了一些错误,我必须获得新的访问令牌。如果我想在墙上张贴一些东西,我需要访问令牌,因此我必须在我的数据库中保留令牌,是的,或者我应该将访问令牌始终保留在我的数据库中?是的,如果您发现一些错误,则需要获取新的访问令牌。并将其保存到数据库中,只需替换为旧数据库即可。就是这样。我已经用和上面一样的方法做了。最好在我的数据库中保留一个60天令牌,对吗?所以当用户登录时,立即获得新的60天令牌?我刚刚编辑了这个问题。使用这种方式,您的访问令牌将永远不会过期。那么我应该在base_facebook中重新声明方法吗?