เพิ่มสมาชิกในรายการ Mailchimp ผ่าน Google App Script

Sep 06 2020

ฉันกำลังพยายามเพิ่มสมาชิกในรายชื่ออีเมล mailchimp โดยอัตโนมัติโดยใช้สคริปต์ของแอป google doc และ google ซึ่งเชื่อมต่อกับ mailchimp API ภายนอก เมื่อใดก็ตามที่ฉันพยายามทดสอบสคริปต์ของฉันฉันได้รับข้อยกเว้น 'ค่าไม่ดี' จาก StackDriver Log

var payload = {
    "email_address": email,
    "status_if_new": "subscribed",
    "merge_fields": {
      FNAME: first_name,
      LNAME: last_name
    },
  };

  var headers = {
    "contentType": "application/json",
    "Authorization": "apikey " + API_KEY,
  };

  var data = {
    "method": "PUT",
    "muteHttpExceptions": false,
    "headers": headers,
    "payload": payload,
  };

  var json = Utilities.jsonStringify(data);
  
  //DEBUG
  Logger.log(json);
  Logger.log(endpoint);

  // call the Mailchimp API
  try {
    var response = UrlFetchApp.fetch(endpoint, json);

    if (response.getResponseCode() === 200) {
      // all good!
      Logger.log("Success");
      Logger.log(response);
    } else {
      Logger.log("Issues");
      Logger.log(response);
    }
  } catch (err) {
    // handle the error here
    Logger.log("Error");
    Logger.log(err);
  }
Sep 5, 2020, 1:02:53 PM Info Exception: Bad value

จากการตรวจสอบเพิ่มเติมเกี่ยวกับ curl ฉันพบว่าการส่งคำขอด้วยตนเองที่มีน้ำหนักบรรทุกและส่วนหัวเดียวกันให้การตอบกลับนี้

curl -X PUT 'https://{server}.api.mailchimp.com/3.0/lists/{my_list}/members/{member_hash}' 
         -H {"Authorization":"apikey {API_KEY}","contentType":"application/json"} 
         -d '{"email_address":"[email protected]","status_if_new":"subscribed","merge_fields":{"LNAME":"test","FNAME":"test"}},"muteHttpExceptions":false}' 

"errors":[{"field":"email_address","message":"This value should not be blank."}]

เหตุใด API จึงไม่รู้จักอีเมลที่ฉันให้ไว้

คำตอบ

2 Diego Sep 06 2020 at 06:49

ปัญหาหลักคือคุณกำลังส่งสตริง JSON เป็นparamsอาร์กิวเมนต์ที่คาดว่าจะมีออบเจ็กต์เข้าUrlFetchApp.fetch(url, params)มา แทนที่จะรวมสตริงทั้งหมดdataคุณจะต้องสตริงเพย์โหลดเท่านั้น (โปรดดูตัวอย่างที่ลิงก์ในเอกสาร Apps Script)

ฉันสงสัยว่าคุณยังจะเผชิญปัญหากับการUtilities.jsonStringify()ที่มีการเลิกใช้ ใช้JSON.stringify()แทน

ฉันยังย้าย contentType ออกจากheadersและเข้าdataเนื่องจากนี่คือวิธีที่ระบุไว้ในfetch()วิธีการ แต่จริงๆแล้วมันไม่มีผลกระทบต่อแอปพลิเคชันของคุณ

var payload = {
  "email_address": email,
  "status_if_new": "subscribed",
  "merge_fields": {
    FNAME: first_name,
    LNAME: last_name
  },
};

var headers = {
  "Authorization": "apiKey " + API_KEY
};

var data = {
  "method": "PUT",
  "muteHttpExceptions": false,
  "headers": headers,
  "contentType": "application/json",
  "payload": JSON.stringify(payload) // The payload should be stringified
};

//DEBUG
Logger.log(endpoint);

// call the Mailchimp API
try {
  var response = UrlFetchApp.fetch(endpoint, data); // pass in data, not json

  if (response.getResponseCode() === 200) {
    // all good!
    Logger.log("Success");
    Logger.log(response);
  } else {
    Logger.log("Issues");
    Logger.log(response);
  }
} catch (err) {
  // handle the error here
  Logger.log("Error");
  Logger.log(err);
}

หากคุณเพียงจะได้รับการเพิ่มสมาชิกแล้วผมอยากแนะนำให้คุณใช้สมาชิกเพิ่มรายการสินค้าที่ปลายทาง ตรงไปตรงมามากขึ้นและไม่จำเป็นต้องให้คุณสร้างแฮชสมาชิก