使用Mailchimp API [英] Use the Mailchimp API

查看:711
本文介绍了使用Mailchimp API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Mailchimp Node.js API href =https://parse.com/docs/hosting_guide =nofollow> Parse Cloud Hosting 应用程序将用户订阅到邮件列表。解析不支持NPM,但鉴于Mailchimp API没有依赖关系,我以为我可以将代码复制到我的项目中。但是,Mailchimp API使用Parse不支持的https模块。

I'd like to use the Mailchimp Node.js API in my Parse Cloud Hosting app to subscribe a user to a mailing list. Parse doesn't support NPM but, given that the Mailchimp API has no dependencies, I thought I'd be able to copy the code into my project. However, the Mailchimp API uses the "https" module which Parse doesn't support.

有人知道这个吗?

推荐答案

我直接使用Mailchimp API,但REST API非常容易使用。

I've been unable to use the Mailchimp API directly but the REST API is pretty easy to use.

main.js 中,创建一个云功能。输入您的API密钥并更新REST URL以指向正确的Mailchimp数据中心( http:// apidocs。 mailchimp.com/api/2.0/

In main.js, create a Cloud Function. Enter your API key and update the REST URL to point at the correct Mailchimp data center (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)

Then, in the code which calls this function... (replace your list 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");
    // ...
});

这篇关于使用Mailchimp API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆