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

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

问题描述

我一直在努力找出与此发生。
我的脚本是一个有点做工精细,突然半停了下来。

我访问API和我找回一个访问令牌。随着访问令牌,我可以访问用户的公共信息就好了。然而,当我尝试发布信息,以自己的FB帐户我得到这个错误。

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

任何想法,这里发生了什么?
我还使用在我的网站的会话跟踪的内部用户ID。不知道如果我的会议可能会造成一个问题。

这是在那里我得到一个错误我上传的脚本。

 要求的Facebook / src目录/ facebook.php';
//创建我们的应用实例(与你的appid和秘密替换此)。
$ Facebook的=新的Facebook(阵列(
  APPID=> '12345678',
  '秘密'=> 已删除,
  '文件上传'=>真正,
  '饼干'=>真正,
));
$ facebook-> setFileUploadSupport(真);$我= NULL;
//基于会话的API调用。
如果($会议){
  尝试{
    $ UID = $ facebook->的getUser();
    $我= $ facebook-> API('/我');
  }赶上(FacebookApiException $ E){
    error_log中($ E);
  }
}
//将根据当前用户状态需要登录或注销的URL。
如果($我){
  $ logoutUrl = $ facebook-> getLogoutUrl();
}其他{
  $ loginUrl = $ facebook-> getLoginUrl();
}
$ photo_details =阵列('信息'=>我的地方);
$文件=照片/ my.jpg'; //例图像文件
$ photo_details ['形象'] ='@'。真实路径($文件);
$ upload_photo = $ facebook-> API('/ ME /照片','后',$ photo_details);


解决方案

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

 要求的Facebook / src目录/ facebook.php';
//创建我们的应用实例(与你的appid和秘密替换此)。
$ Facebook的=新的Facebook(阵列(
  APPID=> APP_ID,
  '秘密'=> APP_SECRET',
));$ USER = $ facebook->的getUser();$ photo_details =阵列('信息'=>我的地方);
$文件=照片/ my.jpg'; //例图像文件
$ photo_details ['形象'] ='@'。真实路径($文件);如果($用户){
  尝试{
    //我们有一个有效的FB会话,因此我们可以用'我'
    $ upload_photo = $ facebook-> API('/ ME /照片','后',$ photo_details);
  }赶上(FacebookApiException $ E){
    error_log中($ E);
  }
}
//将根据当前用户状态需要登录或注销的URL。
如果($用户){
  $ logoutUrl = $ facebook-> getLogoutUrl();
}其他{
//重定向到Facebook登录获得新的用户的access_token
  $ loginUrl = $ facebook-> getLoginUrl();
  头('位置:'。$ loginUrl);
}

我已经写了<一个href=\"http://www.masteringapi.com/tutorials/how-to-upload-a-photo-to-a-users-profile-using-facebook-graph-api/66/\">tutorial如何上传图片到用户的墙。

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天全站免登陆