Javascript + MailChimp API 订阅
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19671676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript + MailChimp API subscribe
提问by barryedmund
When making this request:
提出此请求时:
// Subscribe a new account holder to a MailChimp list
function subscribeSomeoneToMailChimpList()
{
var options =
{
"apikey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"id": "xxxxxx",
"email":
{
"email": "[email protected]"
},
"send_welcome": false
};
var mcSubscribeRequest = UrlFetchApp.fetch("https://us4.api.mailchimp.com/2.0/lists/subscribe.json", options);
var mcListObject = Utilities.jsonParse(mcSubscribeRequest.getContentText());
}
This response is returned:
返回此响应:
Request failed for https://us4.api.mailchimp.com/2.0/lists/subscribe.jsonreturned 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")
https://us4.api.mailchimp.com/2.0/lists/subscribe.json请求失败返回代码 500。截断的服务器响应:{"status":"error","code":-100,"name": "ValidationError","error":"You must specified a apikey value"}(使用 muteHttpExceptions 选项检查完整响应)(第 120 行,文件“v2”)
Line 120 is the line on which UrlFetchApp.fetch
is called.
第 120 行UrlFetchApp.fetch
是被调用的行。
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.
API 密钥是有效的(我已经使用不包含关联数组的更简单的 API 调用进行了测试)。当我将 API 密钥直接附加到基本 URL 并将其从 中删除时options
,我收到一条错误消息,指出列表 ID 无效。当我将列表 ID 直接附加到基本 URL 并将其从 中删除时options
,我收到一条错误消息,指出电子邮件地址必须采用关联数组形式。
My question is: Using the above format, how does one send requests that contain associative arrays?
我的问题是:使用上述格式,如何发送包含关联数组的请求?
The relevant API documentation can be found here.
可以在此处找到相关的 API 文档。
回答by barryedmund
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][email protected]&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.
哪里<dc>
应该替换为 API 密钥中破折号后的部分。例如, “ US1”, “ US2”, “ UK1”,等等。
回答by John Proestakes
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.
在 javascript 中执行此操作会将您的 API 密钥公开给全世界。如果有人拥有您的密钥,他/她可以更改或访问您的帐户。
回答by jjbskir
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)
我想我在阅读 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": "[email protected]"
},
"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.
哪里<dc>
应该替换为 API 密钥中破折号后的部分。例如, “ US1”, “ US2”, “ UK1”,等等。
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.
问题是您的 options 变量应该用作 JSON 而不是 GET url 参数。mailchimp 还指定最好使用 POST 而不是 GET。所以总的来说,你应该确保将你的方法设置为“发布”并确保你的有效负载是有效的 JSON。