一尘不染

facebook Uncaught OAuthException:必须使用活动访问令牌来查询有关当前用户的信息

php

我一直在努力寻找正在发生的事情。我的脚本运行良好,突然停了下来。

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

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

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

这是我的上传脚本,出现错误。

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

阅读 314

收藏
2020-05-26

共1个答案

一尘不染

只需检查当前的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);
}
2020-05-26