通过 Node 和 Mailchimp API 3.0 将成员添加到 Mailchimp 列表

Add Member to Mailchimp list via Node and Mailchimp API 3.0

我想将我的应用程序中的新注册添加到 Mailchimp 列表中。它通过这样的 cURL 语句完美运行:

curl --request POST --url 'https://us4.api.mailchimp.com/3.0/lists/[listid]/members' --user 'anystring:[api key]-us4' --header 'content-type: application/json' --data '{"email_address":"test@example.com", "status":"subscribed","merge_fields":{"FNAME":"Freddie","LNAME":"Jones"}}' --include

我在 Node.js 中使用 Request 模块,如下所示:

var request = require('request');

request({
    url: 'https://us4.api.mailchimp.com/3.0/lists/[list-id]/members',
    user: 'anystring:[api-key]',
    json: {
        "email_address":"test@example.com",
        "user":"anystring:[api-key]",
        "status":"subscribed",
        "merge_fields":{
            "FNAME":"Freddie",
            "LNAME":"Jones"
        }
    },
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    }
}, function(error, response, body){
        if(error) {
            console.log(error);
        } else {
            console.log(response.statusCode, body);
        }
    }
);

但是我得到这个错误:

401 { type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
title: 'API Key Missing',
status: 401,
detail: 'Your request did not include an API key.',
instance: '' }

如何正确制定此请求?

我刚刚与我最近用于访问 Mailchimp 的一些代码进行了比较,发现我提供的 API 键是这样的:

var request = require('superagent'); // I am using SuperAgent

request.post(url)
.set('Authorization', 'apikey ' + apiKey) // this sets a header field
.send(data)
.end(function(err, response) {
    // ...
});

请注意,我使用的是 SuperAgent 而不是 Request 库。不过,您应该能够轻松移植代码段。

本质上,header 字段 Authorization 是字符串 apikey(尾随 space)和实际 API 键的串联。查看请求 documentation,这应该有效:

request({
    url: 'https://us4.api.mailchimp.com/3.0/lists/[list-id]/members',
    json: json,
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'apikey ' + apiKey
    }
}, function(error, response, body) {
    // ...
});

与客户支持来回交流了几次,最后发现我需要将我的 API 密钥添加到 请求 header 以便正确使用 MailChimp v3.0 API 进行身份验证。这是我使用的代码:

request({
    url: 'https://us4.api.mailchimp.com/3.0/lists/[list id]/members',
    json: {
        'email_address': user.email,
        'user': 'anystring:[api key]',
        'status': 'subscribed',
        'merge_fields': {
            'FNAME': user.firstName,
            'LNAME': ''
        }
    },
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'apikey [api key]'
    }
}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        console.log(response.statusCode, '>>>>> USER ADDED TO MAILCHIMP');
        // THEN log the user into the app
        req.login(user, function(err) {
            if (err) {
                res.status(400).send(err);
            } else {
                res.json(user);
            }
        });
    }
});

我在 mailchimp v3 wrapper 中使用请求。我使用以下授权没有问题:

request({
  method : ...,
  url : ...,
  auth : {
    user : 'any',
    password : api_key
  },
  json : ...,
  qs : ...
}