我处于 php 的中级水平,并且是 facebook 开发的新手。我查看了 facebook 文档和 Stack Overflow 之前的评论。
我基本上想要做的就是让用户使用他们的 Facebook 帐户登录并显示他们的名字。
我的 php 页面有一个图表,页面每 2 或 5 分钟自动刷新一次。
我进行身份验证并获取要放在页面上的 facebook first_name。
$graph = $facebook->api('/me');
echo $graph['first_name'] 以获取用户的名字..(我认为不需要访问 token )。
大约 90 分钟后。我一直收到错误:
fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user......
我在 $facebook->getUser(); 中没有值( 0 );参数
我知道离线访问权限已被贬值,(我在我的应用程序高级设置中启用了此功能)
我正在尝试获取扩展访问 token 。在 FB 文档中。我明白了:
https://graph.facebook.com/oauth/access_token?
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN
我在链接中包含了我的信息(一个现有的有效访问 token 和所有)并收到了一个访问 token :
access_token=AAADbZBPuUyWwBAFubPaK9E6CnNsPfNYBjQ9OZC63ZBN2Ml9TCu9BYz89frzUF2EnLttuZAcG2fWZAHbWozrvop9bQjQclxVYle7igvoZCYUAg2KNQLMgNP&expires=4050
然而这个 token 在大约 1 小时左右就过期了。(....expires=4050)
我假设我正在使用服务器端身份验证,因为我正在使用 PHP?
最佳答案
我假设您需要在“应用程序高级设置”页面中启用“弃用 offline_access”。因为这对我有用:
//added code in base_facebook.php inside the facebook class
public function getExtendedAccessToken(){
try {
// need to circumvent json_decode by calling _oauthRequest
// directly, since response isn't JSON format.
$access_token_response =
$this->_oauthRequest(
$this->getUrl('graph', '/oauth/access_token'),
$params = array( 'client_id' => $this->getAppId(),
'client_secret' => $this->getAppSecret(),
'grant_type'=>'fb_exchange_token',
'fb_exchange_token'=>$this->getAccessToken(),
));
} catch (FacebookApiException $e) {
// most likely that user very recently revoked authorization.
// In any event, we don't have an access token, so say so.
return false;
}
if (empty($access_token_response)) {
return false;
}
$response_params = array();
parse_str($access_token_response, $response_params);
if (!isset($response_params['access_token'])) {
return false;
}
return $response_params['access_token'];
}
由于多种原因, token 仍然无效,请参阅 How-To: Handle expired access tokens . 希望对你有帮助
关于facebook-php-sdk - 扩展访问 token 过期不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9107432/