提出这个要求时:的JavaScript + MailChimp API订阅
// 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());
}
此响应返回:
要求失败,返回https://us4.api.mailchimp.com/2.0/lists/subscribe.json代码500截断服务器响应:{ “地位”:“错误(使用muteHttpExceptions选项来检查完整响应)(第120行,文件“v2”)
(“100”,“name”:“ValidationError”,“error”:“您必须指定apikey值”
线120是调用UrlFetchApp.fetch
的线。
API密钥是有效的(我已经测试了更简单的API调用,不包括关联数组)。当我将API密钥直接附加到基本URL并将其从options
中删除时,出现错误,表明列表ID无效。当我然后将列表ID直接附加到基本URL并从options
中删除它时,我收到一个错误消息,说明电子邮件地址必须是关联数组形式。
我的问题是:使用上述格式,如何发送包含关联数组的请求?
相关的API文档可以找到here。
在您的客户端代码中暴露您的API密钥的不好主意:( – Maxim
@Maxim,这个代码在私有Google脚本中使用。这仍然有风险吗? – barryedmund
我不确定Google如何保护它,但我是客人这一切都取决于你需要多少安全性,在某些情况下,我甚至会在服务器端代码中加密密码。 – Maxim