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

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

问题描述

我一直在努力找出这到底是怎么回事.我的脚本运行良好,突然停了一半.

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

我正在访问 API 并取回访问令牌.使用访问令牌,我可以很好地访问用户的公共信息.但是,当我尝试将信息发布到他们的 FB 帐户时,我收到此错误.

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. 

知道这里发生了什么吗?我还在我的网站上使用会话来跟踪内部用户 ID.不确定我的会话是否会导致问题.

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);

推荐答案

只需检查当前 Facebook 用户 ID $user,如果返回 null,则需要重新授权用户(或使用自定义 $_SESSION 用户 ID 值 - 不推荐)

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 Uncaught OAuthException: 必须使用活动访问令牌来查询有关当前用户的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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