php 必须使用活动访问令牌来查询有关当前用户的信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16491737/
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 Cornelia Secelean
I keep getting the error in the title, while using a simple code:
在使用简单的代码时,我不断收到标题中的错误:
require_once('sdk/src/facebook.php');
require_once("AppInfo.php");
function idx(array $array, $key, $default = null) {
return array_key_exists($key, $array) ? $array[$key] : $default;
}
function he($str) {
return htmlentities($str, ENT_QUOTES, "UTF-8");
}
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'sharedSession' => true,
'trustForwarded' => true,
));
$arrayForJSON=array();
$user = $facebook->getUser();
$friends = idx($facebook->api('/me/friends'), 'data', array());
if ($friends) {
$arrayForJSON['friends']=$friends;
}
print_r($friends);
I really don't know how to solve it, I've been searching and trying many answers on stackoverflow
我真的不知道如何解决它,我一直在寻找并尝试在stackoverflow上的许多答案
回答by JayNCoke
You need to set the access token to be able to get data or act on behalf of the user. Are you going through a login flow to allow authorization to your Facebook application?
您需要设置访问令牌才能获取数据或代表用户采取行动。您是否正在通过登录流程来允许对您的 Facebook 应用程序进行授权?
Try the example at https://developers.facebook.com/docs/reference/php/facebook-api/
在https://developers.facebook.com/docs/reference/php/facebook-api/尝试示例
It provides the very basic login flow that will grant access to the data you're seeking.
它提供了非常基本的登录流程,可以授予对您正在寻找的数据的访问权限。
回答by Bahaa Odeh
you need to check if already login if not redirect the user to login link
如果没有将用户重定向到登录链接,您需要检查是否已经登录
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '191149314281714',
'secret' => '73b67bf1c825fa47efae70a46c18906b',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
// $user_profile = $facebook->api('/me');
$friends = idx($facebook->api('/me/friends'), 'data', array());
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'offline_access'));
}