繁体   English   中英

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

[英]Graph API not accepting access token using JavaScript SDK

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

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

此外,我可以使用SDK完全正确的POST调用用户的Feed。 最后,我已经检查以确保我拥有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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM