【问题标题】:Sending the following JSON to Mailchimp API 3.0 -- Getting errors, what am I missing?将以下 JSON 发送到 Mailchimp API 3.0 - 出现错误,我错过了什么?
【发布时间】:2016-07-31 18:40:09
【问题描述】:

我正在尝试将以下 JSON 提交到我的列表中。我正在努力让向我们的 501(c)(3) 慈善机构捐款的人自动通过电子邮件收到用于税收目的的收据,并在我们的“捐赠者”名单上签名,以便我们可以在未来通知他们新的资源和活动.

这是通过 Google 应用脚本 UrlFetchApp.fetch 提交的。为了阅读错误消息,我使用 curl 提交了我的 JSON。我已经回到我几天前尝试的方法,使用 Google 声称我应该提交 JSON 并为 URLFetchApp.fetch 指定特定参数的方式。

{
    "method": "POST",
    "Authorization": "Basic loginname:apikey",
    "Content-Type": "application/json",
    "payload": {
        "status": "subscribed",
        "email_address": "email@example.com",
        "merge_fields": {
            "FNAME": "Tony",
            "LNAME": "Blackmon"
        }
    }
}

方法和授权是为了让 URLFetchApp.fetch 知道它是一个 POST 请求,以及要发送什么身份验证。发送到 MailChimp 的唯一东西是所谓的“有效负载”JSON 对象(根据 Google 文档)。

如果我只使用 curl 提交有效负载对象,它似乎可以正常工作。当我说它似乎工作时,我的列表保持为空,但如果我再次运行 curl 命令,我被告知用户已经在列表中。我等了几分钟,确认用户确实在列表中。

不过,我在运行 Google App 脚本时收到错误消息,这没有任何意义。让我去删除我的用户,然后再次运行它,看看我得到了什么。

【问题讨论】:

    标签: json curl google-apps-script mailchimp


    【解决方案1】:

    想通了...这是我要发送到 MailChimp 的所有代码:

    function sendToMailChimp(first_name, last_name, email_address){
    
      var loginKey = Utilities.base64Encode("bch:" + API_KEY);
    
      var payload = {"status":"subscribed",
                     "email_address":email_address,
                     "merge_fields":{
                          "FNAME":first_name, 
                           "LNAME":last_name}};
    
      var payload = JSON.stringify(payload);
    
      var options = {"method":"POST",
                     "headers":{ "Authorization":"Basic " + loginKey},
                     "Content-Type":"application/json",
                     "muteHttpExceptions" : true,
                     "payload":payload }; 
    
      //var options = JSON.stringify(options);
    
      Logger.log(options);
    
      var response = UrlFetchApp.fetch(mc_base_url,options);
      Logger.log(response);
    
    }
    

    “选项”的注释行 JSON.stringify 不需要发生。它使 URLFetchApp.fetch 弄乱了它的选项。相反,我只需要 JSON.stringify 负载。现在我已经在这上面浪费了两天时间,希望如果其他人遇到这个问题,他们将能够因为这篇文章而解决它。

    【讨论】:

      猜你喜欢
      • 2019-07-30
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 2017-05-21
      • 1970-01-01
      • 2021-01-25
      相关资源
      最近更新 更多