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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6034813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:10:31  来源:igfitidea点击:

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

phpfacebookapiaccess-token

提问by user401183

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

我一直在努力找出这件事发生了什么。我的脚本运行良好,突然停了一半。

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.

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

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

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.

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

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

采纳答案by ifaour

Just check for the current Facebook user id $userand if it returned null then you need to reauthorize the user (or use the custom $_SESSIONuser id value - not recommended)

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

I've written a tutorialon how to upload a picture to the user's wall.

我写了一篇关于如何将图片上传到用户墙的教程

回答by yacon

Use:

用:

$facebook->api('/'.$facebook_uid)

instead of

代替

$facebook->api('/me')

it works.

有用。

回答by Arjay Waran

So I had the same issue, but it was because I was saving the access token but not using it. It could be because I'm super sleepy because of due dates, or maybe I just didn't think about it! But in case anyone else is in the same situation:

所以我遇到了同样的问题,但这是因为我保存了访问令牌但没有使用它。可能是因为到期日我非常困,或者我只是没有考虑过!但万一其他人处于同样的情况:

When I log in the user I save the access token:

当我登录用户时,我保存了访问令牌:

$facebook = new Facebook(array(
    'appId' => <insert the app id you get from facebook here>,
    'secret' => <insert the app secret you get from facebook here>
));

$accessToken = $facebook->getAccessToken();
//save the access token for later


Now when I make requests to facebook I just do something like this:

现在,当我向 facebook 提出请求时,我只是做这样的事情:

$facebook = new Facebook(array(
    'appId' => <insert the app id you get from facebook here>,
    'secret' => <insert the app secret you get from facebook here>
));

$facebook->setAccessToken($accessToken);
$facebook->api(... insert own code here ...)

回答by Jeroen

After a certain amount of time, your access token expires.

一段时间后,您的访问令牌将过期。

To prevent this, you can request the 'offline_access' permission during the authentication, as noted here: Do Facebook Oauth 2.0 Access Tokens Expire?

为了防止这种情况,您可以在身份验证期间请求“offline_access”权限,如下所述:Facebook Oauth 2.0 Access Tokens Expire?

回答by Daniel

Had the same problem and the solution was to reauthorize the user. Check it here:

有同样的问题,解决方案是重新授权用户。在这里检查:

<?php

 require_once("src/facebook.php");

  $config = array(
      'appId' => '1424980371051918',
      'secret' => '2ed5c1260daa4c44673ba6fbc348c67d',
      'fileUpload' => false // optional
  );

  $facebook = new Facebook($config);

//Authorizing app:

?>
<a href="<?php echo $facebook->getLoginUrl(); ?>">Login con fb</a>

Saved project and opened on my test enviroment and it worked again. As I did, you can comment your previous code and try.

保存项目并在我的测试环境中打开,它再次工作。像我一样,您可以评论您以前的代码并尝试。

回答by Vlad

I have got the same issue when tried to get users information without auth.

尝试在没有身份验证的情况下获取用户信息时,我遇到了同样的问题。

Check if you have loggen in before any request.

在任何请求之前检查您是否已登录。

 $uid = $facebook->getUser();

 if ($uid){
      $me = $facebook->api('/me');
 }

The code above should solve your issue.

上面的代码应该可以解决您的问题。

回答by Faisal

I had the same issue but I can solve this issue by cleaning the cookies and cache from my browser (ctrl+shift+R). Also, you must ensure that your access token is active.

我遇到了同样的问题,但我可以通过清除浏览器中的 cookie 和缓存来解决这个问题(ctrl+shift+R)。此外,您必须确保您的访问令牌处于活动状态。

Hope this will help you to solve your problem.

希望这将帮助您解决您的问题。