php - 未捕获的 OAuthException : An active access token must be used to query information about the current user

标签 php facebook api access-token

我一直在努力弄清楚这到底是怎么回事。 我的脚本运行得很好,突然间停止了。

我正在访问 API 并取回访问 token 。使用访问 token ,我可以很好地访问用户的公共(public)信息。但是,当我尝试将信息发布到他们的 FB 帐户时,出现此错误。

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

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

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

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 值 -不推荐)

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

我写了一个 tutorial关于如何将图片上传到用户的墙上。

关于php - 未捕获的 OAuthException : An active access token must be used to query information about the current user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6034813/

相关文章:

c# - 用于 .NET 4.5 中的 dropbox.com 的 API

php - Ajax、PHP、MySQL 检索数据

javascript - FB.getAuthResponse().expiresIn 的单位是什么?

php - 如何制作一个只有 1 个索引的数组到顶层?

没有框架的 JavaScript 树功能

ios - 如何从 iOS 上的 native Facebook 应用程序在 Safari 中打开移动网络应用程序链接?

c# - C# 中的 C++ API 和 PInvoke

linux - 系统API和系统调用API的区别

php - 如何在 PHP ADODB 中回滚事务

php - jquery ui按钮在ajax调用后不出现