我一直在努力弄清楚这到底是怎么回事。 我的脚本运行得很好,突然间停止了。
我正在访问 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/