的Facebook引入了一个新的端点,允许开发者通用接入令牌(〜2小时寿命的)延伸到60天的令牌。它是作为发送HTTP GET来简单:
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
的响应是位靠不住(相对于它们的正常JSON响应),所以准备解析响应。我选择了PHP的parse_url函数。
// url to curl (note: make sure you pass in the correct values for your app
// and the user access token you'd like to exchange.
$url = 'https://graph.facebook.com/oauth/access_token?client_id=$fb_app_id&client_secret=$fb_app_secret&grant_type=fb_exchange_token&fb_exchange_token=$access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
// parse response
parse_str($response, $token_data);
// exchanged token
$access_token = $token_data['access_token'];
echo 'exchanged access token: ' . $access_token;
一旦你的交换令牌,头向Facebook的访问令牌调试器来检查你的代码是否正常工作。如果正确交换,到期日期应为当前的60。
https://developers.facebook.com/tools/debug
如果你担心你的访问令牌到期,您可以检查运行时到期,如果到期时间的临近呼唤一个全新60天使用令牌。每次用户访问时,效率较低(但更容易)的方法是交换您的令牌。
请注意这个支持很快就会被弃用,在2012年10月3日,offline_access权限将被移除' – ajreal 2012-08-28 04:58:50