Facebook的。致命错误:未捕获OAuthException:必须使用活动的访问令牌来查询有关当前用户的信息 [英] Facebook. Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user
问题描述
我之前有其他应用程序(在用户墙上发布文本)的问题,但在我添加
$ user = $ facebook-> ; getUser();
这里有什么问题?我已经添加了 offline_access
权限...请帮助我,如果可以的话,非常感谢。 <?php
require_once('images / Facebook.php');
$ facebook = new Facebook(array(
'appId'=>'456080124457246',
'secret'=>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
)) ;
$ user_profile = $ facebook-> api('/ me','GET');
$ accessToken = $ facebook-> getAccessToken();
#获取用户ID
$ user = $ facebook-> getUser();
$ facebook-> setAccessToken($ accessToken);
$ facebook-> api(456080124457246);
if($ user){
try {
#图片说明
$ photoCaption = $ user_profile ['name']。 'patarimųplaukamssužinojočiahttp://goo.gl/otwhf';
#您的图像绝对路径。
$ imageUrl ='http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; //示例URL
#发布照片API的数据
$ post_data = array(
'message'=> $ photoCaption,
'url'=> $ imageUrl
);
$ apiResponse = $ facebook-> api('/ me / photos','POST',$ post_data);
} catch(FacebookApiException $ e){
error_log($ e);
}
} else {
$ loginUrl = $ facebook-> getLoginUrl(array(
'scope'=>'publish_stream,photo_upload'
));
echo(< script> top.location.href ='$ loginUrl。';< / script>);
}
?>
在这种情况下, - > getUser()调用 - 一旦达到此行就抛出异常:
$ user_profile = $ facebook- > API( '/我', 'GET');
$ facebook-> api()是一件棘手的事情,因为它抛出一个例如,如果不知道/ me是谁,即使您稍后尝试修复它也是如此。
我认为,要包装整个事情在尝试... catch块。像这样:
<?php
require_once('images / facebook.php');
$ facebook = new Facebook(array(
'appId'=>'456080124457246',
'secret'=>'xxxxx',
)) ;
尝试{
$ user_profile = $ facebook-> api('/ me','GET');
#获取用户ID
$ user = $ facebook-> getUser();
if($ user){
try {
#照片说明
$ photoCaption = $ user_profile ['name']。 'patarimųplaukamssužinojočiahttp://goo.gl/otwhf';
#您的图像绝对路径。
$ imageUrl ='http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; //示例URL
#发布照片API的数据
$ post_data = array(
'message'=> $ photoCaption,
'url'=> $ imageUrl
);
$ apiResponse = $ facebook-> api('/ me / photos','POST',$ post_data);
} catch(FacebookApiException $ e){
error_log($ e);
}
} else {
$ loginUrl = $ facebook-> getLoginUrl(array(
'scope'=>'publish_stream,photo_upload'
));
echo(< script> top.location.href ='$ loginUrl。';< / script>);
}
} catch(异常$ e){
$ loginUrl = $ facebook-> getLoginUrl(array(
'scope'=>'publish_stream, photo_upload'
));
echo(< script> top.location.href ='$ loginUrl。';< / script>);
}
?>
将用户重定向到登录网址几乎立即,随后回来一切。您不需要使用此设置设置accessToken。
这可能实际上不必要地重复了一些功能,但希望这是开始的。
顺便说一句,值得 offline_access权限正在淘汰。
with my app's administrator acount on facebook my app work normally, but with other account I get error: Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
I had this problem before with other app (publish text on the user's wall), but fixed after i added
$user = $facebook->getUser();
What's wrong here? I have added offline_access
permission... Help me, please if you can, Thank you very much.
<?php
require_once('images/Facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user_profile = $facebook->api('/me','GET');
$accessToken = $facebook->getAccessToken();
# Get User ID
$user = $facebook->getUser();
$facebook->setAccessToken($accessToken);
$facebook->api(456080124457246);
if ($user) {
try {
# Photo Caption
$photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
In this case it's not even getting to your $facebook->getUser() call -- it's throwing an exception as soon as it reaches this line:
$user_profile = $facebook->api('/me','GET');
$facebook->api() is a bit of a tricky thing to work with because it throws an exception immediately if it doesn't know who "/me" is...even if you try to fix it later.
The trick, I think, is to wrap the entire thing in a try...catch block. Like so:
<?php
require_once('images/facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'xxxxx',
));
try {
$user_profile = $facebook->api('/me','GET');
# Get User ID
$user = $facebook->getUser();
if ($user) {
try {
# Photo Caption
$photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
} catch (Exception $e) {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
That'll redirect the user to the login url pretty much immediately, then come back with everything in tow. You don't have to set the accessToken with this setup.
This may actually unnecessarily repeat some functionality, but hopefully it's something to start with.
By the way, for what it's worth the offline_access permission is being phased out.
这篇关于Facebook的。致命错误:未捕获OAuthException:必须使用活动的访问令牌来查询有关当前用户的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!