Facebook.致命错误:未捕获的 OAuthException:必须使用活动访问令牌来查询有关当前用户的信息 [英] Facebook. Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user

查看:28
本文介绍了Facebook.致命错误:未捕获的 OAuthException:必须使用活动访问令牌来查询有关当前用户的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我的应用程序在 facebook 上的管理员帐户,我的应用程序正常工作,但使用其他帐户时出现错误:致命错误:未捕获的 OAuthException:必须使用活动访问令牌来查询有关当前用户的信息.我之前在使用其他应用程序时遇到过这个问题(在用户墙上发布文本),但在我添加后修复了$user = $facebook->getUser(); 这里有什么问题?我已经添加了 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>");
      }
    ?>

推荐答案

在这种情况下,它甚至没有到达您的 $facebook->getUser() 调用——它在到达这一行时立即抛出异常:

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() 使用起来有点棘手,因为如果它不知道/me"是谁,它会立即抛出异常……即使您稍后尝试修复它.

$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.

我认为,诀窍是将整个事物包装在 try...catch 块中.像这样:

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

?>

<小时>

这会立即将用户重定向到登录 URL,然后返回所有内容.您不必使用此设置来设置 accessToken.


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.

顺便说一下,offline_access 权限正在逐步淘汰.

这篇关于Facebook.致命错误:未捕获的 OAuthException:必须使用活动访问令牌来查询有关当前用户的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆