Mailchimp RESTful API 3.0 HTTP 基本认证

Mailchimp RESTful API 3.0 HTTP Basic Auth

我正在尝试使用具有基本身份验证的 Mailchimp API 3.0 版。我使用的是经典 ASP.

Json 响应总是:"API Key Missing"。

Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.open "GET", "https://us4.api.mailchimp.com/3.0/", False
HttpReq.setRequestHeader "Content-Type", "application/json"
HttpReq.setRequestHeader "apikey", "xxxxxx"
HttpReq.send ""
Response.Write  HttpReq.ResponseText
Set HttpReq = Nothing

我将其作为 header 发送。

我做错了什么..?

答案是:

HttpReq.setRequestHeader "Authorization", "apikey xxxxxx"

如果您尝试使用基本身份验证,则实际上需要 follow the spec. You can build the header yourself using the wiki article as your guide, but the easiest thing is to just use your HTTP Library's built-in support for that. In your case, this will probably help

要自己滚动,您需要两条信息。第一个是用户名,第二个是密码。对于 MailChimp v3.0,用户名可以是任何内容。我倾向于使用 'apikey' 作为用户名。密码是 API 密钥本身。假设我的 API 密钥是 'xxxxxxxxxx-yyy'。现在,您对字符串 apikey:xxxxxxxxxx-yyy 进行 Base 64 编码。这给了我 YXBpa2V5Onh4eHh4eHh4eHgteXl5。现在我创建的 header 是:

Authorization: Basic YXBpa2V5Onh4eHh4eHh4eHgteXl5

您正在使用的方法会起作用,但它是 MailChimp 的自定义方法,可能会使未来的访问者对您的代码感到困惑。

我看到问题是基于#C 但我在 java 中遇到了同样的问题。(我是 java 的新手所以可以随意编辑我的代码)。

public String get(String url) throws IOException {
     HttpGet get = new HttpGet(url);
     String apiEncode = "apikey:9590e52MyAPI8-us9";
     String encoding = Base64.encodeBytes(apiEncode.getBytes());                     
     get.setHeader("Authorization","Basic " + encoding );
     HttpResponse response = http.execute(get);

    if (response.getEntity() != null) {
        return EntityUtils.toString(response.getEntity(), "UTF-8").trim();
    } else {
        throw new IOException(response.getStatusLine().toString());
    }
}