拥有以下代码:
$appsecretProof = hash_hmac('sha256', $shortLivedToken, $secret);
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php-3.2');
//get extended user access token
$url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token' .
'&client_id=' . $appId .
'&client_secret=' . $secret .
'&fb_exchange_token=' . $shortLivedToken .
'&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
$response_params = [];
parse_str($curlResult, $response_params);
$extendedUserToken = $response_params['access_token'];
$appsecretProof = hash_hmac('sha256', $extendedUserToken, $secret);
//get extended page access token
$url = 'https://graph.facebook.com/' . $pageId .
'?fields=access_token' .
'&access_token=' . $extendedUserToken .
'&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
curl_close($ch);
$pageToken = json_decode($curlResult)->access_token;
如何使用常规Facebook SDK实现相同目标?谷歌搜索并没有真正帮助。要么我发现什么都没有用,要么解决方案不起作用,抛出错误,例如"需要访问令牌才能请求此资源"或类似的东西。我已经尝试了很多,但没有什么真正有效。
我的最后一种方法是:
FacebookSession::setDefaultApplication($appId, $secret);
$shortLivedSession = new FacebookSession($shortLivedToken);
$shortLivedSession->getLongLivedSession();
$request = new FacebookRequest($shortLivedSession, 'GET', '/' . $pageId . '?fields=access_token');
$response = $request->execute();
$result = $response->getGraphObject()->asArray();
$pageToken = $result['access_token'];
$facebookSession = new FacebookSession($pageToken);
此代码始终返回一个短期令牌。
答案 0 :(得分:0)
想出来和@luschn,你博客上的代码似乎有同样的错误。
有助于取代这两条线:
$shortLivedSession->getLongLivedSession();
$request = new FacebookRequest($shortLivedSession, 'GET', '/' . $pageId . '?fields=access_token');
用这些:
$longLivedSession = $shortLivedSession->getLongLivedSession();
$request = new FacebookRequest($longLivedSession, 'GET', '/' . $pageId . '?fields=access_token');