【问题标题】:How I can do facebook batch fql in java我如何在java中做facebook批处理fql
【发布时间】:2011-10-08 12:03:10
【问题描述】:

在 facebook fql 中有这个代码

https://developers.facebook.com/docs/reference/api/batch/


curl \
    -F 'access_token=…' \
    -F 'batch=[ \
            {"method": "GET", "relative_url": "me"}, \
            {"method": "GET", "relative_url": "me/friends?limit=50"} \
        ]'\
    https://graph.facebook.com

它应该与 json 一起发送 但我真的不明白该怎么做 有什么帮助吗?

谢谢

【问题讨论】:

  • 你的问题能更准确一点吗?你尝试了什么,你期望什么,你得到了什么结果?你试过上面的命令吗?它失败了吗?你有安装卷曲吗?它有反应吗?你得到了什么?你看到了什么错误信息?

标签: java facebook batch-file facebook-fql


【解决方案1】:

您可以简单地使用BatchFB api,它非常强大和简单,您不必处理所有这些东西,它使用 fql 例如让你所有的朋友

Later<ArrayNode> friendsArrayList = this.Batcher.query("SELECT uid FROM user WHERE uid  IN (SELECT uid2 FROM friend WHERE uid1 = me())");
    for (JsonNode friend : friendsArrayList.get()) {
            .......
        }

及其批处理

【讨论】:

    【解决方案2】:

    我相信您的问题是如何使用 Facebook Graph API 执行批处理请求。为此,您必须向

    发出 POST 请求
    "https://graph.facebook.com" 
    

    要发送的post数据应该是

    "batch=[{'method': 'GET', 'relative_url': 'me'}, {'method': 'GET', 'relative_url': 'me/friends?limit=50'}]&access_token=@accesstoken" 
    

    在您的情况下 [@accesstoken 必须替换为您的访问令牌值]。

    此请求将返回访问令牌所有者的详细信息(通常是当前登录的用户)和用户的 50 个 facebook 好友列表(包含 id 和 name 字段)以及页面标题(可以省略) .

    我不确定您是指 java 还是 Javascript。请具体说明。

    我基本上是一名 C# 程序员。将在此处为您提供在 C# 中执行上述请求的代码。

    WebRequest webRequest = WebRequest.Create("https://graph.facebook.com");
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-UrlEncoded";
    byte[] buffer = Encoding.UTF8.GetBytes("batch=[{'method': 'GET', 'relative_url': 'me'}, {'method': 'GET', 'relative_url': 'me/friends?limit=50'}]&access_token=@ACCESSTOKEN");
    webRequest.ContentLength = buffer.Length;
    using (Stream stream = webRequest.GetRequestStream())
    {
        stream.Write(buffer, 0, buffer.Length);
        using (WebResponse webResponse = webRequest.GetResponse())
        {
            if (webResponse != null)
            {
                using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {
                    string data = streamReader.ReadToEnd();
                }
            }
        }
    }
    

    这里的变量 data 将包含结果。

    【讨论】:

      【解决方案3】:

      Salah,这是我用作参考的示例,虽然我不记得在哪里找到了,但我很抱歉。

      FB.api("/", "POST", {
          access_token:"MY_APPLICATION_ACCESS_TOKEN",
          batch:[
              {
                  "method":"GET",
                  "name":"get-photos",
                  "omit_response_on_success": true,
                  "relative_url":"MY_ALBUM_ID/photos"
              },
              {
                  "method": "GET",
                  "depends_on":"get-photos",
                  "relative_url":"{result=get-photos:$.data[0].id}/likes"
              }
          ]
      }, function(response) {
          if (!response || response.error) {
              console.log(response.error_description);
          } else {    
              /* Iterate through each Response */
              for(var i=0,l=response.length; i<l; i++) {
                  /*  If we have set 'omit_response_on_success' to true in the Request, the Response value will be null, so continue to the next iteration */
                  if(response[i] === null) continue;
                  /*  Else we are expecting a Response Body Object in JSON, so decode this */
                  var responseBody = JSON.parse(response[i].body);
                  /*  If the Response Body includes an Error Object, handle the Error */
                  if(responseBody.error) {
                      // do something useful here
                      console.log(responseBody.error.message);
                  } 
                  /*  Else handle the data Object */
                  else {
                      // do something useful here
                      console.log(responseBody.data);
                  }
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-28
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多