Graph API不接受使用JavaScript SDK的访问令牌

Graph API not accepting access token using JavaScript SDK

本文关键字:SDK 访问令牌 JavaScript API 不接受 Graph      更新时间:2024-01-17

我在这个问题上绞尽脑汁已经很久了。加载JavaScript SDK后,我无法对图形API进行任何GET调用。我试图使用/me/home,但出于调试目的,我也尝试过/me。奇怪的是,如果我检查用户的登录状态,它返回的是一个访问令牌,我可以使用它在地址栏中完美地检索新闻源。然而,当我使用JavaScript对Graph API进行GET调用时,我得到:

"必须使用活动访问令牌来查询有关当前用户的信息。"

此外,我还可以使用SDK使POST调用用户的提要非常好。最后,我进行了检查,以确保我具有read_stream权限。

window.fbAsyncInit = function() {
  FB.init({
    appId      : '<?php echo($facebook_key); ?>', // App ID
    channelUrl : '//'+window.location.hostname+'/channel.php', // Channel File
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });
  // listen for and handle auth.statusChange events
  FB.Event.subscribe('auth.statusChange', function(response) {
    if (response.authResponse) {
      // user has auth'd your app and is logged into Facebook
      console.log(response);
    } else {
      location.href='welcome.php';
    };
  });
  FB.getLoginStatus(function(response) {
    console.log(response);
  });
  //get Facebook news feed
  FB.api('/me/home', 'get', function(response) {
    console.log(response);
  });
};
// Load the SDK Asynchronously
(function(d){
  var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
  if (d.getElementById(id)) {return;}
  js = d.createElement('script'); js.id = id; js.async = true;
  js.src = "//connect.facebook.net/en_US/all.js";
  ref.parentNode.insertBefore(js, ref);
}(document));
//status update function
function post (form) {
  var status = form.status.value;
  FB.api('/me/feed', 'post', { message: status }, function(response) {
    if (!response || response.error) {
      alert('Error occured');
    } else {
      alert('Status updated');
    };
  });
};

我会尝试将您对我的呼叫转移到您的FB.getLoginStatus函数中:

FB.getLoginStatus(function(response) {
   if (response.status === 'connected') {
    //get Facebook news feed
     FB.api('/me/home', 'get', function(response) {
       console.log(response);
     });
  }
 });

我认为问题是,在确认身份验证之前,您对api的调用已经启动。希望将此函数移到getLoginStatus回调中可以解决此问题。

你可以在这里看到更多的例子https://developers.facebook.com/tools/console/