打开图形:Java脚本:必须使用活动访问令牌来查询有关当前用户的信息

Open Graph : Java Script : active access token must be used to query information about the current user

本文关键字:查询 信息 用户 访问令牌 活动 Java 图形 脚本      更新时间:2023-09-26

谁能告诉我下面的开放图形调用代码有什么问题。

我已经尝试过使用应用程序令牌,用户令牌,并且每次响应我时都没有任何令牌

{"

error":{"message":"必须使用活动访问令牌来查询有关当前用户的信息。","type":"OAuthException","code":2500}}

我已经使用FB调试器验证了应用程序令牌,用户令牌和我的代码

<?php
$appId = '****************';
$appSecret = '*************************';
$appNameSpace = 'og_loctest';
$token_url =    "https://graph.facebook.com/oauth/access_token?" .
        "client_id=" . $appId .
        "&client_secret=" . $appSecret .
        "&grant_type=client_credentials";
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
?>
<html xmlns:og="http://ogp.me/ns" xmlns:fb="http://www.facebook.com/2008/fbml" lang="en">
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# og_loctest: http://ogp.me/ns/fb/og_loctest#">
  <meta property="fb:app_id" content="<?php echo $appId;?>" /> 
  <meta property="og:type"   content="og_loctest:properties" /> 
  <meta property="og:url"    content="http://nirav-metronew.kwetoo.com/fbtest/" /> 
  <meta property="og:title"  content="Sample Properties" /> 
  <meta property="og:image"  content="https://s-static.ak.fbcdn.net/images/devsite/attachment_blank.png" />
  <title>OG Tutorial App</title>
</head>
<body>
  <div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : "<?php echo $appId;?>",
        status: true,
        cookie: true,
        xfbml: true,
        frictionlessRequests: true,
        useCachedDialogs: true,
        oauth: true
      });
    };
    // Load the SDK Asynchronously
    (function(d){
      var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
      js = d.createElement('script'); js.id = id; js.async = true;
      js.src = "//connect.facebook.net/en_US/all.js";
      d.getElementsByTagName('head')[0].appendChild(js);
    }(document));
    function fblogin()
    {
         FB.login(function(response) {
           if (response.authResponse) {
             postProperties();
           }
         },{scope: 'email,offline_access,publish_stream,publish_actions'});
    }
      function postProperties()
      {
          FB.api(
            '/me/og_loctest:post',
            'post',
            { properties: 'http://nirav-metronew.kwetoo.com/fbtest/',access_token:"<?php echo $access_token;?>"},
            function(response) {
               if (!response || response.error) {
                  alert('Error occured');
               } else {
                  alert('Facebook open graph tested! Action ID: ' + response.id);
               }
            });
      }  
  </script>
</head>
<body>
  <form>
    <input type="button" value="Post properties" onclick="fblogin()" />
  </form>
</body>
</html>

任何人都可以告诉我下面的开放图形调用代码有什么问题。

您的脚本逻辑存在缺陷 - 因为您混淆了客户端和服务器端发生的事情以及何时发生。

      FB.api(
        '/me/og_loctest:post',
        'post',
        { properties: 'http://nirav-metronew.kwetoo.com/fbtest/',
          access_token:"<?php echo $access_token;?>"},

您正在FB.login的回调中调用此客户端。如果用户刚刚登录,那么之前已经在您的服务器上运行的 PHP 无法知道刚刚导致客户端登录的当前访问令牌。PHP无法展望未来...因此,无论最终在这个地方发布什么,它肯定不是您认为的当前访问令牌。

但是 JS SDK 完全能够处理它的访问令牌本身,因此您不必将其作为参数显式传递到此 API 调用中 - 只需完全删除部分access_token:"…"即可。

相关文章: