【问题标题】:How to fix the MailChimp 401 error which appears in hyper terminal from the mail-chimp api? (using JavaScript)如何从 mail-chimp api 修复超级终端中出现的 MailChimp 401 错误? (使用 JavaScript)
【发布时间】:2022-05-16 02:34:28
【问题描述】:

我正在尝试制作一个注册表单,当用户在该表单中输入他的名字、第二名和电子邮件时,他将登录,我可以在 Mailchimp 的受众部分看到他的姓名和电子邮件.

完成我的代码后(几乎),我尝试执行它,但什么也没发生。它一直在加载,所以我回到我的代码,复制了我的列表 ID 的链接,我使用 nodemon(一个为自动加载终端而设计的节点包),它通过终端告诉我:

{
  type: 'https://mailchimp.com/developer/marketing/docs/errors/',
  title: 'API Key Invalid',
  status: 401,
  detail: "Your API key may be invalid, or you've attempted to access the wrong datacenter.",
  instance: 'c97f4b31-f9df-d2bc-6bb7-ab1a6ad5874b'
}

我的 HTML 代码:

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Hugo 0.87.0">
    <title>NewsLetter Sign-Up</title>

    <link rel="canonical" href="https://getbootstrap.com/docs/5.1/examples/sign-in/">



    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">


    <!-- Favicons -->
    <link rel="apple-touch-icon" href="/docs/5.1/assets/img/favicons/apple-touch-icon.png" sizes="180x180">
    <link rel="icon" href="/docs/5.1/assets/img/favicons/favicon-32x32.png" sizes="32x32" type="image/png">
    <link rel="icon" href="/docs/5.1/assets/img/favicons/favicon-16x16.png" sizes="16x16" type="image/png">
    <link rel="manifest" href="/docs/5.1/assets/img/favicons/manifest.json">
    <link rel="mask-icon" href="/docs/5.1/assets/img/favicons/safari-pinned-tab.svg" color="#7952b3">
    <link rel="icon" href="/docs/5.1/assets/img/favicons/favicon.ico">
    <meta name="theme-color" content="#7952b3">


    <style>
        .bd-placeholder-img {
            font-size: 1.125rem;
            text-anchor: middle;
            -webkit-user-select: none;
            -moz-user-select: none;
            user-select: none;
        }
        
        @media (min-width: 768px) {
            .bd-placeholder-img-lg {
                font-size: 3.5rem;
            }
        }
    </style>


    <!-- Custom styles for this template -->
    <link href="styles.css" rel="stylesheet">
</head>

<body class="text-center">


    <form action="/" class="form-signin" method="post">
        <img class="mb-4" src="images/gul code.png" alt="" width="72" height="57">

        <h1 class="h3 mb-3 fw-normal">Signup to My NewsLetter!</h1>

        <input type="text" name="fName" class="form-control top" id="floatingInput" placeholder="First Name" required autofocus>
        <input type="text" name="lName" class="form-control middle" id="floatingPassword" placeholder="Second Name" required>
        <input type="text" name="email" class="form-control bottom" id="floatingPassword" placeholder="E-mail" required>


        <button class="w-100 btn btn-lg btn-primary" type="submit">Sign Me Up!</button>
        <p class="mt-5 mb-3 text-muted">&copy; YourCode</p>
    </form>

</body>

</html>

HTML 文档的样式.css:

html,
body {
    height: 100%;
}

body {
    display: flex;
    align-items: center;
    padding-top: 40px;
    padding-bottom: 40px;
    background-color: #f5f5f5;
}

.form-signin {
    width: 100%;
    max-width: 330px;
    padding: 15px;
    margin: auto;
}

.form-signin .form-floating:focus-within {
    z-index: 2;
}

.top {
    margin-bottom: -1px;
    border-bottom-right-radius: 0;
    border-bottom-left-radius: 0;
}

.middle {
    border-top-left-radius: 0;
    border-bottom-right-radius: 0;
}

.bottom {
    margin-bottom: 10px;
    border-top-left-radius: 0;
    border-top-right-radius: 0;
}

最后,我的 JavaScript:

const express = require("express");
const request = require("request");
const bodyParser = require("body-parser");
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;

    const data = {
        members: [{
            email_address: email,
            status: "subscribed",
            merge_fields: {
                FNAME: firstName,
                LNAME: lastName
            }
        }]
    };

    const jsonData = JSON.stringify(data);
    const url = "https://us5.api.mailchimp.com/3.0/lists/{list_id}";
    const options = {
        method: "POST", // here
        auth: "legion1:myapikey"
    }



    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 is running on port 3000");
});

我在互联网上和 Stack Overflow 中查找了许多问题,但没有一个能解决此错误。有很多 Stack Overflow 问题对我没有用。

【问题讨论】:

  • 您的url 有效吗?看起来{list_id} 只是一个字符串而不是https://us6.api.mailchimp.com/3.0/lists/${list_id},我在您的代码中没有看到list_id 变量。
  • 我使用了 List_id 这个词而不是我的实际 ID,对于 https://us6.api.mailchimp.com/3.0/lists/${list_id},它给出了错误详细信息:“您的 API 密钥可能无效,或者您尝试访问错误的数据中心。” ,```.
  • 尝试将此添加到您的选项中:headers: { 'Authorization': 'Bearer &lt;TOKEN&gt;' }
  • 我尝试将其添加到我的选项中,但现在我收到“403”错误,其中详细信息为"The API key provided is linked to datacenter 'us5&gt;'",。这是什么意思??我只知道它不承认我是授权用户。
  • 也许这对你有帮助? stackoverflow.com/a/41888321/9618749

标签: javascript html node.js mailchimp mailchimp-api-v3.0


【解决方案1】:

也许我来不及帮助你,但我正在和你做同样的课程(Angela Yu 博士 - 全栈 Web 开发人员训练营),这是我的代码:

const express = require("express")
const mailchimp = require("@mailchimp/mailchimp_marketing")
const app = express()
const https = require("https")


app.use(express.static(__dirname))
app.use(express.urlencoded({extended: true}))

//Setting up MailChimp

mailchimp.setConfig({
//API KEY
 apiKey: "xxxxxxxxxxxxxxxxxxxxxx-usxx",
//API KEY PREFIX (THE SERVER)
  server: "usxx"
})

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_address: email,
        status: "subscribed",
        merge_fields: {
          FNAME: firstName,
          LNAME: lastName}
      }]
  }

  const jsonData = JSON.stringify(data)
  
  const url = "https://usxx.api.mailchimp.com/3.0/lists/(**YOUR LIST ID HERE**)"
  
  const options = {
    method: "POST",
    auth:'anyStringYouWant:xxxxxxxxxxxxxxx-usxx' 
    //Enter the API key
  }
  
  const request = https.request(url, options, function(response) {
    if (response.statusCode === 200) {
      res.sendFile(__dirname + "/success.html")
    } else {
      res.sendFile(__dirname + "/failure.html")
    }

    response.on("data", function (data){
      console.log(JSON.parse(data))
        })
      })

      //comment the request to test the de failure page
      request.write(jsonData)
      request.end()

    })

app.post("/failure", function (req, res) {
  res.redirect("/")
})

app.listen(3000, () => console.log("Server is running on port 3000"))

使用此代码,它不会在终端上显示任何错误,并在 mailchimp 列表中输入新联系人。

【讨论】:

    猜你喜欢
    • 2017-05-17
    • 2022-06-21
    • 1970-01-01
    • 2019-07-30
    • 2023-03-21
    • 2016-01-10
    • 2012-07-19
    • 2019-07-06
    • 1970-01-01
    相关资源
    最近更新 更多