Facebook Graph API - 所有用户使用相同的访问令牌
长期以来,我一直在疯狂地尝试为我的用户获取访问令牌。我在许多网站上阅读,通过使用 getSession() 并从中获取 access_token 以某种方式获取它...它给了我未定义的函数错误..我也用谷歌搜索了这一点,所有解决方案都说使用更新的 SDK,但我的已经更新了,它仍然行不通...所以我终于得到了另一种解决方案来获取访问令牌,但这似乎为所有用户提供了相同的访问令牌...知道问题出在哪里吗?所有用户肯定不能拥有相同的令牌,对吗?
$app_id = $facebook->getAppId();
$app_secret = $facebook->getApiSecret();
function callFb($url, $params)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$params=array('client_id'=>$app_id, 'type'=>'client_cred', 'client_secret'=>$app_secret);
$url = "https://graph.facebook.com/oauth/access_token";
$access_token = callFb($url, $params);
$access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token));
I have been madly trying to get access token for my users since long. I read at many sites to get it somehow by using getSession() and getting access_token from it... it gives me undefined function error.. I googled that too and all solutions said to use the updated SDK but mine is updated and it still won't work... so I finally got another solution to getting access token but this seems to be giving same access token for all users... any idea where the problem lies? All users certainly can't have same token right?
$app_id = $facebook->getAppId();
$app_secret = $facebook->getApiSecret();
function callFb($url, $params)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$params=array('client_id'=>$app_id, 'type'=>'client_cred', 'client_secret'=>$app_secret);
$url = "https://graph.facebook.com/oauth/access_token";
$access_token = callFb($url, $params);
$access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您要求
type=client_cred
,这告诉 Facebook 您不需要用户的访问令牌,而是应用程序的访问令牌。这用于执行诸如访问见解、实时更新 API 和公共数据之类的操作。如果您想获取用户数据,则不应传递该标志。如果您确实想自行访问 Graph API,您当然可以按照 https://developers.facebook.com/docs/authentication/。
The issue is that you are asking for
type=client_cred
, which tells Facebook that you don't want access token for a user, but an access token for the app. This is used for doing things like accessing insights, the realtime updates API, and public data. If you want to get user data, you should not be passing that flag.If you really do want to roll your own access to the Graph API, you can certainly do that, following the instructions at https://developers.facebook.com/docs/authentication/ .
您说您正在使用 PHP SDK,但我在代码中没有看到任何提及它的地方。
正确的方法是这样的:
示例大部分转自 Facebook 的 SDK Github。
You say you're using the PHP SDK but I don't see any mention of it anywhere in your code.
The proper way is this:
Example largely paraphrased from Facebook's SDK Github.