FacebookSession::setDefaultApplication('1578373582426403', 'CENSORED');
$session = FacebookSession::newAppSession('1578373582426403', 'CENSORED');
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject();
调用$request->execute()
时,我收到错误:
必须使用活动访问令牌来查询有关当前用户的信息。
我不确定我做错了什么......有人可以帮忙吗?
答案 0 :(得分:2)
如果您尝试访问当前用户的信息,请尝试此操作。更改重定向登录
// Call Facebook Session- App Id, App Secret
Facebook\FacebookSession::setDefaultApplication('1578373582426403', 'CENSORED');
//Facebook Redirect Helper
//Create a FB variable, Pass oAuth URL
$facebook= new Facebook\FacebookRedirectLoginHelper('http://localhost/fb/');
//
try
{
//To check method getSessionFromRedirect
if($session= $facebook->getSessionFromRedirect())
{
//store the token in a session variable
$_SESSION['facebook']=$session->getToken();
}
//If the session is already set
if(isset($_SESSION['facebook']))
{
//New Facebook Session; Pass the facebook token
$session= new \Facebook\FacebookSession($_SESSION['facebook']);
//Creating a fb request, pass the session, method, /me to get the user's details
$request= new \Facebook\FacebookRequest($session,'GET','/me');
//Execute the request
$request=$request->execute();
$user=$request->getGraphObject()->asArray();
// print_r($user);
}
}
catch(Facebook\FacebookRequestException $e)
{
//When FB returns an error
}
catch(\Exception $e)
{
//Normal exception
}
答案 1 :(得分:0)
您正在使用App Access令牌设置会话。这意味着访问令牌仅标识您的应用;没有用户。
您正在进行的通话<form id='registerForm' name='registerForm' method='post' action="" >
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" required minlength="2" />
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" required />
</div>
<div>
<input type="submit" name="submit" value="Submit"/>
</div>
</form>
<script type="text/javascript" src="jq213.js"></script>
<script type="text/javascript" src="validate.min.js"></script>
<script>
$(function() {
$("#registerForm").validate({
rules:{
name:{
required:true,
minlength:2
},
email:{
required:true,
email:true
},
},
messages:{
name:{
required:"pls enter your name",
minlength:"minimum 2 char for name"
},
email:{
required:"required",
email:"Enter a valid email"
}
}
});
});
</script>
是所用访问令牌的当前用户 ID的别名。但是,应用程序没有用户ID。而且,只使用App Access令牌,您就无法请求用户信息。
要使您的脚本正常工作,您应该查询/me
,这是应用程序ID。
更新:您还可以查询/1578373582426403
,它应该是访问令牌应用的别名。