【问题标题】:Trouble building FB.api query构建 FB.api 查询时遇到问题
【发布时间】:2014-11-15 18:25:40
【问题描述】:

好的,所以我正在开发一个加载更多结果的 Facebook 群组供稿,但我在构建初始查询以使其全部正常工作时遇到了麻烦。

在下面的第一个if 语句中,您可以看到我将查询部分放入变量的位置,然后调用函数,传递这些变量。这一切都很好......

if (response.status === 'connected') {
// Logged into your app and Facebook.
console.log('Welcome!  Fetching your information.... ');

var path = '/', 
    method = 'POST',
    params = {
      batch: [
        {method : 'GET', name : 'user', relative_url : '/me?fields=id,name,picture'},
        {method: 'GET', name : 'post-ids', relative_url: '/group-id/feed?fields=fields{with field expansion}',omit_response_on_success : false}
      ]
    };

loadFeed(path, method, params);

}

下面的功能是我遇到麻烦的地方。第一次调用该函数时,我需要将这三个变量合二为一,并使用 FB.api 调用它。你可以在这里看到函数:

function loadFeed(path, method, params) {

    console.log('------------------');
    console.log(path + ', ' + method + ', ' + params);

    if(path != 'undefined') {
      if(method != 'undefined') {
        if(params != 'undefined') { var query = '{\'' + path + '\', \'' + method + '\', ' + params + '}'; } 
      } 
      else { var query = path; }
    } 

    $('#load-more').css('display', 'hidden');

    FB.api(query, function (response) {
        console.log(response);

      // first time page loads, response[0] is the login, and response[1] is the feed
      // each time after that, response[0] is the feed

      if(response.length > 1) {
        var membody = JSON.parse(response[0].body),
            feed = JSON.parse(response[1].body);
      } else {
        var feed = JSON.parse(response);
      }

      if(feed.paging) {

        if(feed.paging.next) {

          var load_more = '<div id="load-more"><center>Load more</center></div>',
              method = '',
              params = '';

          $('#feed').append(load_more);

          $('#load-more').click( function() {
            loadFeed(feed.paging.next);
          });

        }

      }

    });
}

在第一次调用这个函数时,我得到这个错误:

error: Object
    code: 2500
    message: "Unknown path components: /', 'POST', [object Object]}"
    type: "OAuthException"

这似乎告诉我,我基本上把查询放在一起是错误的,但我尝试了一些不同的方法,但都没有奏效。您可以在错误消息中看到查询开头缺少单引号,而我无法弄清楚如何将单引号保留在那里。

有人对我如何解决这个问题有任何想法吗?

另外,如果您知道更好的方法来完成这一切,那么我也将不胜感激!

【问题讨论】:

    标签: javascript facebook pagination facebook-javascript-sdk facebook-sdk-4.0


    【解决方案1】:

    您似乎正在使用 HTTP API 参数构建您的 Javascript API 调用。

    为用户查询JS API:

    FB.api(
      "/me", // or "/99999999999" the user's id
      function(response) {
        if (response && !response.error) {
          /* handle the result */
        }
    
    );
    

    来源:https://developers.facebook.com/docs/graph-api/reference/v2.2/user

    查询组的JS API:

    FB.api(
      "/{group-id}",
      function(response) {
        if (response && !response.error) {
          /* handle the result */
        }
      }
    );
    

    来源:https://developers.facebook.com/docs/graph-api/reference/v2.2/group

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2014-07-09
      相关资源
      最近更新 更多