我想在我的 Parse Cloud Hosting应用程序中使用 Mailchimp Node.js API来订阅用户到邮件列表. Parse不支持NPM,但鉴于Mailchimp API没有依赖关系,我认为我可以将代码复制到我的项目中.但是,Mailchimp AP
有没有人知道这方面的方法?
我一直无法直接使用Mailchimp API,但REST API非常易于使用.在main.js中,创建一个云函数.输入您的API密钥并更新REST URL以指向正确的Mailchimp数据中心(http://apidocs.mailchimp.com/api/2.0/)
var mailchimpApiKey = "<<REPLACE_WITH_YOUR_KEY>>"; Parse.Cloud.define("SubscribeUserToMailingList", function(request, response) { if (!request.params || !request.params.email){ response.error("Must supply email address, firstname and lastname to Mailchimp signup"); return; } var mailchimpData = { apikey : mailchimpApiKey, id : request.params.listid, email : { email : request.params.email }, merge_vars : request.params.mergevars } var url = "https://<<REPLACE_WITH_DATA_CENTRE>>.api.mailchimp.com/2.0/lists/subscribe.json"; Parse.Cloud.httpRequest({ method: 'POST', url: url, body: JSON.stringify(mailchimpData), success: function(httpResponse) { console.log(httpResponse.text); response.success("Successfully subscribed"); }, error: function(httpResponse) { console.error('Request failed with response code ' + httpResponse.status); console.error(httpResponse.text); response.error('Mailchimp subscribe failed with response code ' + httpResponse.status); } }); });
然后,在调用此函数的代码中…(替换您的列表ID)
Parse.Cloud.run("SubscribeUserToMailingList", { listid : "<<REPLACE_WITH_LIST_ID>>", email : email, mergevars : { FNAME : firstName, LNAME : lastName } }) .then(function(success){ console.log("Successfully subscribed"); // ... }, function(error){ console.log("Unable to subscribe"); // ... });