用于更新mailchimp成员信息的Google脚本从API获得了确定的响应,但未更新任何信息

时间:2018-11-11 06:20:49

标签: mailchimp mailchimp-api-v3.0

目标是在此处进行任何更新时在mailchimp上更新成员的信息。我已经可以使用onEdit函数了。 Mailchimp API结果正常,并且显示“节流=否”,响应= n / a。 Google控制台的响应也可以,所以我很生气:

    function sendToMailChimp(id_customer,id_shop,id_gender,fn,ln,em,optin,date_add,last_order,num_pedidos,aov,good_experience){
  var API_KEY = 'xxxxxxxx-us10';
  var mc_base_url = 'https://us10.api.mailchimp.com/3.0';
  var mc_list_id = 'xxxxxxxxxxxx';

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('');
    //Header of PUT request
    var headers = {
     'Authorization': 'Basic ' + Utilities.base64Encode('username' + ':' + API_KEY, Utilities.Charset.UTF_8),

    };

    //MailChimp API requires MD5 hash of email to identify a subscriber
    function md5(str) {
        return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, str).reduce(function(str,chr){
        chr = (chr < 0 ? chr + 256 : chr).toString(16);
        return str + (chr.length==1?'0':'') + chr;
        },'');
    }
    var emHash = md5(em.toString().toLowerCase());

    //Body of PUT request
    var payload =  JSON.stringify({ 
      "status_if_new": "subscribed", 
      "email_address": em,
      "merge_fields": {
        "id_customer": id_customer,
        "id_shop": id_shop,
        "id_gender": id_gender,
        "optin": optin,
        "date_add": date_add,
        "last_order": last_order,
        "num_pedidos": num_pedidos,
        "aov": aov,
        "firstname": fn, 
        "lastname": ln,
        "good_experience": good_experience
      }
    });

  //put the above variables together into 'options'
  var options = {
    "headers" : headers,
    "payload": payload,
    "method" : "put",
    "muteHttpExceptions" : true
  };
  //PUT request to specific subscriber in Mailchimp
  var response = UrlFetchApp.fetch(mc_base_url + '/lists/' + mc_list_id + '/members/' + emHash,options);
  if( response.getResponseCode() == 200 ) {
    console.info( "SUCCESS to PROD for " + em + payload);
  } else {
    console.log(response);
  }
}

0 个答案:

没有答案