简体   繁体   English

如何将 API 密钥添加到 Axios 对 mailchimp 的发布请求

[英]How to add API key to Axios post request for mailchimp

I'm trying to set up an axios post request to add members to an audience list, but I can't figure out how to add the API key (keeps giving error 401: 'Your request did not include an API key.').我正在尝试设置 axios 发布请求以将成员添加到受众列表,但我不知道如何添加 API 密钥(不断给出错误 401:“您的请求不包括 ZDB97442387143ACE146 密钥。” . I've tried a bunch of things in the "Authorization" header, like what I put below (also: "Bearer ${mailchimpKey}" , "${mailchimpKey}" , "Bearer ${mailchimpKey}" , "Basic ${mailchimpKey}" , and probably more...).我在“授权”header 中尝试了很多东西,就像我在下面放的一样(还有: "Bearer ${mailchimpKey}""${mailchimpKey}""Bearer ${mailchimpKey}""Basic ${mailchimpKey}" ,可能还有更多......)。

I also don't know what the "username" would be, but "any" worked when I tested the API elsewhere.我也不知道“用户名”是什么,但是当我在其他地方测试 API 时,“任何”都有效。

Does anyone know how I should set this up?有谁知道我应该如何设置?

axios
.post(
  `https://${server}.api.mailchimp.com/3.0/lists/${list_id}/members`,
  {
    email_address: email,
    status: "subscribed",
  },
  {
    "User-Agent": "Request-Promise",
    Connection: "keep-alive",
    Authorization: `Basic any:${mailchimpKey}`,
    // Testing on localhost
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Headers": "Content-Type",
  }
)

If your intention is to use HTTP Basic authentication , just use the Axios auth config option如果您打算使用HTTP 基本身份验证,只需使用 Axios auth配置选项

axios.post(
  `https://${server}.api.mailchimp.com/3.0/lists/${encodeURIComponent(list_id)}/members`,
  {
    email_address: email,
    status: "subscribed",
  },
  {
    auth: {
      username: "anystring",
      password: mailchimpKey
    },
    headers: { // personally, I wouldn't add any extra headers
      "User-agent": "Request-Promise"
    }
  }
)

HTTP Basic auth headers look like HTTP 基本身份验证标头看起来像

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

where the string after "Basic" is the Base64 encoded "username:password" string.其中“基本”之后的字符串是 Base64 编码的"username:password"字符串。 Axios provides the auth option as a convenience so you don't need to encode the string yourself. Axios 提供auth选项是为了方便,因此您不需要自己对字符串进行编码。

Some other problems you had were:您遇到的其他一些问题是:

  • Adding request headers outside the headers config optionheaders配置选项之外添加请求标头
  • Attempting to send Access-Control-Allow-Origin and Access-Control-Allow-Headers as request headers.尝试发送Access-Control-Allow-OriginAccess-Control-Allow-Headers作为请求标头。 These are response headers only.这些只是响应标头。 Adding them to your request will most likely cause more CORS errors将它们添加到您的请求中很可能会导致更多 CORS 错误

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

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