php 必须使用活动访问令牌来查询有关当前用户的信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7342266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An active access token must be used to query information about the current user
提问by Eae
I have defined an access token. It is a fresh access token obtained with curl. Why am I still getting the error: An active access token must be used to query information about the current user.
我已经定义了一个访问令牌。它是使用 curl 获得的新访问令牌。为什么我仍然收到错误:必须使用活动访问令牌来查询有关当前用户的信息。
<?php
define('YOUR_APP_ID', '*****');
define('YOUR_APP_SECRET', '*****');
function get_facebook_cookie($app_id, $app_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);
$url = "https://graph.facebook.com/oauth/access_token";
$client_id = "214620421927216";
$client_secret = "1dc2110b1cdbbdf8604edc4ee7b03b81";
$postString = "client_id=$client_id&client_secret=$client_secret&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
$response = curl_exec($curl);
print_r($response);
echo "</br>";
$access_token = substr($response,13);
echo $access_token;
echo "</br>";
$url = 'https://graph.facebook.com/me?access_token=' . $access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$user = json_decode($response);
print_r($user);
?>
<html>
<body>
<?php if ($cookie) { ?>
Welcome <?php ?>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= YOUR_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
</body>
</html>
回答by Eddy Chan
the token that you tried to get in the above is the app level access token. in order to query "/me" via graph api, you need to get user access token.
您尝试在上面获得的令牌是应用程序级别的访问令牌。为了通过图形 API 查询“/me”,您需要获取用户访问令牌。
check the user login section in FB doc: http://developers.facebook.com/docs/authentication/
检查 FB 文档中的用户登录部分:http: //developers.facebook.com/docs/authentication/