简体   繁体   English

使用 Mailchimp 的 API 将订阅者添加到列表

[英]Adding subscribers to a list using Mailchimp's API

I just started this all API thing- I'm trying to upload data to Mailchimp using their API.我刚刚开始这一切 API 的事情 - 我正在尝试使用他们的 API 将数据上传到 Mailchimp。 I'm not getting any error while running my server, but the data won't get updated (I do get the values from the html form and they get displayed in the hyper terminal) that's my code运行我的服务器时我没有收到任何错误,但数据不会更新(我确实从 html 表单中获取了值,并且它们显示在超级终端中)这是我的代码

const bodyParser=require("body-parser");
const request=require("request");
const https=require("https");

const app=express();

app.use(express.static("public")); 
app.use(bodyParser.urlencoded({extended:true}));

app.get("/",function(req,res)  
{
    res.sendFile(__dirname+"/signup.html");
});

app.post("/",function(req,res)  
{
    const firstName=req.body.Fname;
    const lastName=req.body.Lname;
    const email=req.body.email;

    console.log(firstName,lastName,email);

    const data=
        {
            members:[
                {
                    email_adress:email,
                    status:"subscribed",
                    merge_fields:
                        {
                            FNAME:firstName,
                            LNAME:lastName
                        }
                }
            ]
        };
    const jsonData=JSON.stringify(data);

    const url="https://us8.api.mailchimp.com/3.0/lists/b8e36ded50"; 
    const options=
        {
            methods:"POST",
            auth:"lidor:56599763339503b7bac161913940d98d-us8" 
        }
    const request=https.request(url,options,function(response)
            {
                response.on("data",function(data)
                        {
                            console.log(JSON.parse(data));
                        })
            })
    request.write(jsonData);
    request.end();


});

app.listen(3000,function()
{
    console.log("server has started on port 3000");
});

please help me !!请帮我 !!

Try this instead试试这个

var mailchimpInstance   = 'your_prefix_server',
    listUniqueId        = 'your_listId',
    mailchimpApiKey     = 'your_api_key';

app.post('/signup', function (req, res) {
    request
        .post('https://' + mailchimpInstance + '.api.mailchimp.com/3.0/lists/' + listUniqueId + '/members/')
        .set('Content-Type', 'application/json;charset=utf-8')
        .set('Authorization', 'Basic ' + new Buffer.from('any:' + mailchimpApiKey ).toString('base64'))
        .send({
          'email_address': req.body.email,
          'status': 'subscribed',
          'merge_fields': {
            'FNAME': req.body.firstName,
            'LNAME': req.body.lastName
          }
        })
            .end(function(err, response) {
              if (response.status < 300 || (response.status === 400 && response.body.title === "Member Exists")) {
                res.send('Signed Up!');
              } else {
                res.send('Sign Up Failed :(');
              }
          });
});

from https://www.codementor.io/@mattgoldspink/integrate-mailchimp-with-nodejs-app-du10854xp来自https://www.codementor.io/@mattgoldspink/integrate-mailchimp-with-nodejs-app-du10854xp

also he uses "superagent" module instead of "request" module.他还使用“superagent”模块而不是“request”模块。 It works!有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM