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

pro*_*ngs 7 facebook facebook-graph-api facebook-javascript-sdk facebook-access-token

在我的代码中我有

FB.api('/me/friends', function(response) {
            if(response.data) {
                //TODO : what to do if no. of friends is more than 5000 (pagination by fb)
                friends_data=response.data;
                dijit.registry.byId("mainWidget_div").set_friends_data(friends_data);
            } else {
                alert("Error!");
            }

          });
Run Code Online (Sandbox Code Playgroud)

这给出了一个错误.但是,如果我手动调用此函数(在控制台上),则没有错误

FB.api('/me/friends', function(response){r=response;});
//wait a while
r
Run Code Online (Sandbox Code Playgroud)

现在r.data是我的一群朋友.

我检查了网络面板,我收集到,当我手动调用它时,访问令牌会自动插入到请求URL中,当通过代码调用它时,访问令牌不会被插入.

我的应用程序中的完整fb sdk加载代码是这样的:

<script type="text/javascript">
      // You probably don't want to use globals, but this is just example code
      var fbAppId = "{{facebook_app_id}}";

      // This is boilerplate code that is used to initialize the Facebook
      // JS SDK.  You would normally set your App ID in this code.

      // Additional JS functions here
      window.fbAsyncInit = function() {
        FB.init({
          appId      : fbAppId,        // App ID
          status     : true,           // check login status
          cookie     : true,           // enable cookies to allow the server to access the session
          xfbml      : true            // parse page for xfbml or html5 social plugins like login button below
        });

        // Put additional init code here
        dojo.ready(function(){
          FB.api('/me/friends', function(response) {
            if(response.data) {
                //TODO : what to do if no. of friends is more than 5000 (pagination by fb)
                friends_data=response.data;
                dijit.registry.byId("mainWidget_div").set_friends_data(friends_data);
            } else {
                alert("Error!");
            }

          });
        });
      };
      // Load the SDK Asynchronously
      (function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/en_US/all.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));

    </script>
Run Code Online (Sandbox Code Playgroud)

Bre*_*ley 2

我的猜测是,您正在尝试在 Facebook API 完全初始化之前获取好友列表。您看到的错误是什么?

您正在注册 FB.api 调用以在 DOM 就绪 (dojo.ready) 上运行。这可能会导致它加载不同步,即使它全部包含在 fbAsyncInit 中。Friends API 调用本身不依赖于 DOM,因此我不会将其包装在 dojo 调用中。您没有在控制台中执行此操作,但它正在工作。

我不是 JavaScript 专家。如果我的猜测可能不正确,那么发生这种情况的原因可能与 javascript 提升有关。