facebook未捕获OAuthException:必须使用活动的访问令牌来查询有关当前用户的信息 [英] facebook Uncaught OAuthException: An active access token must be used to query information about the current user

查看:140
本文介绍了facebook未捕获OAuthException:必须使用活动的访问令牌来查询有关当前用户的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力找出发生了什么事情。
我的脚本有一点工作正常,突然半停了。



我正在访问api并且恢复访问令牌。使用访问令牌,我可以访问用户的公共信息很好。但是,当我尝试发布信息到他们的FB帐户我得到这个错误。

 致命错误:未捕获OAuthException:活动访问必须使用令牌来查询有关当前用户的信息。 

任何想法这里发生了什么?
我还在我的网站上使用会话跟踪内部用户ID。不知道我的会话是否可能导致问题。



这是我的上传脚本,我收到错误。

  require'facebook / src / facebook.php'; 


//创建我们的应用程序实例(将其替换为您的appId和密码)。
$ facebook = new Facebook(array(
'appId'=>'12345678',
'secret'=>'REMOVED',
'fileUpload'=> true,
'cookie'=> true,
));
$ facebook-> setFileUploadSupport(true);

$ me = null;
//基于会话的API调用。
if($ session){
try {
$ uid = $ facebook-> getUser();
$ me = $ facebook-> api('/ me');
} catch(FacebookApiException $ e){
error_log($ e);
}
}


//根据当前用户状态,需要登录或注销url。
if($ me){
$ logoutUrl = $ facebook-> getLogoutUrl();
} else {
$ loginUrl = $ facebook-> getLoginUrl();
}


$ photo_details = array('message'=>'我的地方');
$ file ='照片/ my.jpg'; //示例图像文件
$ photo_details ['image'] ='@'。真实路径($文件);
$ upload_photo = $ facebook-> api('/ me / photos','post',$ photo_details);


解决方案

只需检查当前的Facebook用户ID $ user 如果它返回null,那么您需要重新授权用户(或使用自定义的 $ _ SESSION 用户ID值 - 不推荐)

  require'facebook / src / facebook.php'; 


//创建我们的应用程序实例(将其替换为您的appId和密码)。
$ facebook = new Facebook(array(
'appId'=>'APP_ID',
'secret'=>'APP_SECRET',
));

$ user = $ facebook-> getUser();

$ photo_details = array('message'=>'我的地方');
$ file ='照片/ my.jpg'; //示例图像文件
$ photo_details ['image'] ='@'。真实路径($文件);

if($ user){
try {
//我们有一个有效的FB会话,所以我们可以使用'我'
$ upload_photo = $ facebook- &api('/ me / photos','post',$ photo_details);
} catch(FacebookApiException $ e){
error_log($ e);
}
}


//根据当前用户状态,需要登录或注销url。
if($ user){
$ logoutUrl = $ facebook-> getLogoutUrl();
} else {
//重定向到Facebook登录以获取新用户access_token
$ loginUrl = $ facebook-> getLoginUrl();
header('Location:'。$ loginUrl);
}

我写了一个教程一张照片给用户的墙。


I've been struggling to find out what is happening with this. My scripts were working fine for a bit and suddenly half stopped.

I'm accessing the api and am getting back an access token. With the access token, I can access a users public info just fine. However, when I try to post info to their FB account I get this error.

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. 

Any idea what is happening here? I'm also using sessions on my site to keep track of internal user ids. Not sure if my sessions could be causing a problem.

This is my upload script where I'm getting an error.

require 'facebook/src/facebook.php';


// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '12345678',
  'secret' => 'REMOVED',
  'fileUpload' => true, 
  'cookie' => true,
));
$facebook->setFileUploadSupport(true); 

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}


// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}


$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
$upload_photo = $facebook->api('/me/photos', 'post', $photo_details);

解决方案

Just check for the current Facebook user id $user and if it returned null then you need to reauthorize the user (or use the custom $_SESSION user id value - not recommended)

require 'facebook/src/facebook.php';


// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
));

$user = $facebook->getUser();

$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);

if ($user) {
  try {
    // We have a valid FB session, so we can use 'me'
    $upload_photo = $facebook->api('/me/photos', 'post', $photo_details);
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}


// login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
// redirect to Facebook login to get a fresh user access_token
  $loginUrl = $facebook->getLoginUrl();
  header('Location: ' . $loginUrl);
}

I've written a tutorial on how to upload a picture to the user's wall.

这篇关于facebook未捕获OAuthException:必须使用活动的访问令牌来查询有关当前用户的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆