我正在尝试在我的个人网站中建立一个部分,显示我在Facebook上的帖子中标记的位置。我的理解是,我需要在服务器上执行oAuth请求才能获得访问令牌:
更新
我构建了一个 php 文件,该文件获取一个包含令牌的文件并刷新它。我可以每小时使用 cron 作业调用此文件,并且拥有无限的令牌。它看起来像这样:
$appID = "APPID";
$appSecret = "APPSECRET";
$currentToken = file_get_contents(__DIR__."/token.txt");
$url = "https://graph.facebook.com/oauth/access_token?client_id=".$appID."&client_secret=".$appSecret."&grant_type=fb_exchange_token&fb_exchange_token=".$currentToken;
$ci = curl_init();
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ci, CURLOPT_TIMEOUT, 10);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_FOLLOWLOCATION, TRUE);
$newtoken = curl_exec($ci);
curl_close ($ci);
$newtoken = str_replace("access_token=","",$newtoken);
$newtoken = substr($newtoken, 0,strpos($newtoken, "&expires=",0));
if($newtoken == "")
{
echo "error";
}
else
{
$file = __DIR__."/token.txt";
file_put_contents($file, $newtoken);
echo $newtoken;
}
如果访问令牌的格式为 {app_id}|{app_secret}
,则使用的不是用户访问令牌,而是应用访问令牌。
似乎您没有实施正确的登录过程来收集用户访问令牌。坚持使用提供的 FB PHP SDK 示例代码。
看
- https://developers.facebook.com/docs/php/gettingstarted/4.0.0
- https://developers.facebook.com/docs/facebook-login/access-tokens#usertokens