我的代码无法正常工作,我无法弄清楚为什么,它一直给我一个401,表示缺少API密钥,所以我不知道这是怎么回事,我想弄清楚我的问题是什么在这段代码上?
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
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){
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var email = req.body.email;
var data = {
members: [
{
email_address: email,
status: "subscribed"
}
]
};
var jsonData = JSON.stringify(data);
var options = {
url: "https://us20.api.mailchimp.com/3.0/lists/listId"
method: "POST",
headers: {
"Authorization": "mkouk24 Api Key"
},
body: jsonData
};
request(options, function(error,response,body){
if (error) {
console.log(error);
} else {
console.log(response.statusCode);
}
});
});
app.listen(3000, function() {
console.log("Server is running on port 3000!");
});
答案 0 :(得分:0)
首先,根据文档,我认为您需要使用app.use(bodyParser.json())
才能在后期请求中传递参数。
第二,您应该从MailChimp生成API令牌并将其添加到此处Authorization": "mkouk24 Api Key"
有关此链接的更多信息:https://mailchimp.com/help/about-api-keys/