【问题标题】:Facebook API batch request with Google Apps Script使用 Google Apps 脚本的 Facebook API 批处理请求
【发布时间】:2015-07-27 12:54:55
【问题描述】:

我正在努力在 Apps 脚本中构建批处理请求,以便在一次 API 调用中更改多个 Facebook 广告集预算。

Facebook 给了我下面的例子(curl):

curl -F 'access_token=____' 
    -F 'batch=[
               {
                "method": "POST",
                "relative_url": "<API_VERSION>/6004251715639",
                "body": "redownload=1&bid_info={\"clicks\":100}"
               },
               {
                "method": "POST",
                "relative_url": <API_VERSION>/v6004251716039",
                "body": "redownload=1&bid_info={\"clicks\":100}"
               },
               {
                "method": "POST",
                "relative_url": "<API_VERSION>/6004251715839",
                "body": "redownload=1&bid_info={\"clicks\":100}"
               }
              ]' https://graph.facebook.com

我在 Apps 脚本中的代码如下:

function testBatchRequest() {

  var url = "https://graph.facebook.com/v2.3/?access_token=XXX",
        batch=[

               {
                "method": "POST",
                "relative_url": "/<AD SET ID>",
                "body": "redownload=1&daily_budget=25000"
               },
               {
                "method": "POST",
                "relative_url":"/<AD SET ID>",
                "body": "redownload=1&daily_budget=25000"
               }];

  url = url + "&batch=" + JSON.stringify(batch);
  Logger.log(url);

  var options = {
      "method": 'POST',
      "followRedirects" : true,
      "muteHttpExceptions": true
  };

  var result = UrlFetchApp.fetch(url, options);


  var json = result.getContentText();
  var data = JSON.parse(json);

  Logger.log(json);
  Logger.log(data); 
}

作为响应,我得到“无效参数”。

回答潜在的 SDK 问题:我还没有找到将 JavaScript SDK 集成到应用脚本中的有效方法。

这是我第一次处理批处理请求,我仍然认为自己是初学者。任何帮助,将不胜感激。谢谢

【问题讨论】:

  • 我相信当您对包括方括号在内的整个字符串进行编码时,可能会发生这种情况。试试 batch={......} 和 JSON.strinigy() 那个字符串。

标签: facebook facebook-graph-api google-apps-script


【解决方案1】:

这需要相当多的令人沮丧的试验和错误,因为请求在 Postman 中可以正常粘贴,但解决方案是批处理变量需要在字符串化的基础上进行 URL 编码。

所以如果你这样做,上面的代码将起作用

 url = url + "&batch=" + encodeURIComponent( JSON.stringify(batch) );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多