Nice programing

facebook Uncaught OAuthException : 현재 사용자에 대한 정보를 쿼리하려면 활성 액세스 토큰을 사용해야합니다.

nicepro 2020. 12. 30. 20:22
반응형

facebook Uncaught OAuthException : 현재 사용자에 대한 정보를 쿼리하려면 활성 액세스 토큰을 사용해야합니다.


나는 이것으로 무슨 일이 일어나고 있는지 알아 내기 위해 고군분투하고 있습니다. 내 스크립트가 약간 잘 작동하고 갑자기 절반이 중지되었습니다.

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

현재 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 tutorial on how to upload a picture to the user's wall.


Use:

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

instead of

$facebook->api('/me')

it works.


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 = 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 ...)

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?


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.


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.


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.

Hope this will help you to solve your problem.

ReferenceURL : https://stackoverflow.com/questions/6034813/facebook-uncaught-oauthexception-an-active-access-token-must-be-used-to-query-i

반응형