Javascript + MailChimp API订阅 [英] Javascript + MailChimp API subscribe
问题描述
发出此请求时:
// Subscribe a new account holder to a MailChimp list
function subscribeSomeoneToMailChimpList()
{
var options =
{
"apikey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"id": "xxxxxx",
"email":
{
"email": "me@example.com"
},
"send_welcome": false
};
var mcSubscribeRequest = UrlFetchApp.fetch("https://us4.api.mailchimp.com/2.0/lists/subscribe.json", options);
var mcListObject = Utilities.jsonParse(mcSubscribeRequest.getContentText());
}
返回此响应:
https://us4.api.mailchimp.com/2.0/lists/subscribe的请求失败. json 返回了代码500.服务器响应被截断:{"status":"error","code":-100,"name":"ValidationError","error":您必须指定apikey值"} (使用MutantHttpExceptions选项检查完整响应)(第120行,文件"v2")
Request failed for https://us4.api.mailchimp.com/2.0/lists/subscribe.json returned code 500. Truncated server response: {"status":"error","code":-100,"name":"ValidationError","error":"You must specify a apikey value"} (use muteHttpExceptions option to examine full response) (line 120, file "v2")
第120行是调用UrlFetchApp.fetch
的行.
Line 120 is the line on which UrlFetchApp.fetch
is called.
API密钥有效(我已经用不包含关联数组的更简单的API调用进行了测试).当我将API密钥直接附加到基本URL并将其从options
删除时,我收到一条错误消息,指出列表ID无效.然后,当我将列表ID直接附加到基本URL并将其从options
删除时,我收到一条错误消息,指出电子邮件地址必须为关联数组形式.
The API key is valid (I have tested with simpler API calls that don't include associative arrays). When I append the API key directly to the base URL and remove it from the options
, I get an error saying that the list ID is invalid. When I then append the list ID directly to the base URL and remove it from options
, I get an error saying that the email address must be in associative array form.
我的问题是:使用以上格式,一个如何发送包含关联数组的请求?
My question is: Using the above format, how does one send requests that contain associative arrays?
相关API文档可在此处找到.
The relevant API documentation can be found here.
推荐答案
经过进一步研究&修修补补,我能够解决这个问题:
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
<dc>
的位置应替换为API密钥中破折号后的部分.例如" us1 "," us2 "," uk1 "等
Where <dc>
should be replaced with the portion after the dash in your API Key. e.g. "us1", "us2", "uk1", etc.
这篇关于Javascript + MailChimp API订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!