Javascript + MailChimp API subscribe

后端 未结 3 1521
误落风尘
误落风尘 2021-02-02 01:30

When making this request:

// Subscribe a new account holder to a MailChimp list
function subscribeSomeoneToMailChimpList()
{
  var options =
  {
    \"apikey\":          


        
相关标签:
3条回答
  • 2021-02-02 02:02

    I think I figured out what is going on after reading through the UrlFetchApp.fetch Docs. https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app?csw=1#fetch(String)

    It looks like you should be using some of the extra params to do the request such as payload and method. Your options variable should look like this.

      var payload =   {
        "apikey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "id": "xxxxxx",
        "email": {
          "email": "me@example.com"
        },
        "send_welcome": false
      };
      payload = Utilities.jsonStringify(payload); // the payload needs to be sent as a string, so we need this
      var options = {
         method: "post",
         contentType: "application/json", // contentType property was mistyped as ContentType - case matters
         payload: payload
      }; 
      var result = UrlFetchApp.fetch("https://<dc>.api.mailchimp.com/2.0/lists/subscribe.json", options);
    

    Where <dc> should be replaced with the portion after the dash in your API Key. e.g. "us1", "us2", "uk1", etc.

    The issue is that your options variable is suppose to be used as JSON and not as a GET url parameter. Also mailchimp specifies that it is better to use POST instead of GET. So over all you should make sure to set you method to "post" and make sure your payload is valid JSON.

    0 讨论(0)
  • 2021-02-02 02:09

    Doing this in javascript exposes your API key to the world. If someone has your key, he/she can make changes to or gain access to your account.

    0 讨论(0)
  • 2021-02-02 02:10

    After further research & tinkering, I was able to solve this:

    https://<dc>.api.mailchimp.com/2.0/lists/subscribe.json?apikey=<my_api_key>&id=<my_list_id>&email[email]=test@test.com&merge_vars[FNAME]=John&merge_vars[LNAME]=Doe&double_optin=false&send_welcome=false
    

    Where <dc> should be replaced with the portion after the dash in your API Key. e.g. "us1", "us2", "uk1", etc.

    0 讨论(0)
提交回复
热议问题