调用与.net MailChimp API V3.0 [英] Calling MailChimp API v3.0 with .Net

查看:676
本文介绍了调用与.net MailChimp API V3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过新的3.0 REST API来访问我们的MailChimp帐户。我已经做了以下内容:

I'm trying to access our MailChimp account via the new 3.0 REST API. I've done the following:

using(var http = new HttpClient())
{
    var creds = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:mailchimpapikey-us1"));
    http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", creds);
    string content = await http.GetStringAsync(@"https://us1.api.mailchimp.com/3.0/lists");
    Console.WriteLine(content);
}



然而,当我运行此代码,我得到一个401错误有以下JSON细节:

However, when I run this code, I get a 401 error with the following json details:

{"type":"http://kb.mailchimp.com/api/error-docs/401-api-key-invalid","title":"API Key Invalid","status":401,"detail":"Your API key may be invalid, or you've attempted to access the wrong datacenter.","instance":"a9fe4028-519e-41d6-9f77-d2caee4d4683"}

我使用的数据中心在我的URI(在本实施例US1)在我的API密钥直流匹配。如果我使用MailChimp SDK,所以我知道我的钥匙是不是我的无效API密钥的工作原理。此外,使用招,我可以看到,MailChimp SDK呼吁正如我在URI正在做相同的DC。

The datacenter I'm using in my URI (us1 in this example) matches the dc on my API key. My API key works if I use the MailChimp SDK so I know my key isn't invalid. Also, using Fiddler, I can see that the MailChimp SDK is calling the same dc as I'm doing in my URI.

任何想法,为什么我有麻烦认证?

Any Ideas as to why I am having trouble Authenticating?

修改
作为问题指出,我专门询问有关访问新的3.0 REST API。我试图直接做到这一点,而不是使用第三方包装。

EDIT As noted in the question, I'm asking specifically about accessing the new 3.0 REST API. I'm trying to do this directly as opposed to using a third party wrapper.

新的API是由HTTP来电所以它应该是相当直截了当。我只是有与认证件麻烦了。

The new API is composed of http calls so it should be pretty straight forward. I'm simply having trouble with the authentication piece.

推荐答案

所以我能最后用超级聊天< /在MailChimp STRONG>技术支持人员。

So I was able to finally chat with a super tech support person at MailChimp.

MailChimp文档载明下列

The MailChimp docs state the following

The easiest way to authenticate is using HTTP Basic Auth. Enter any string
as the username and supply your API Key as the password.
Your HTTP library should have built-in support for basic authorization.



他们的文档是有点误导。通常情况下,验证头中基本认证将是什么样子,我送:

Their documentation is a bit misleading. Typically the Auth header for Basic Auth would look like what I was sending:

Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

其中,x行会代表base64编码的用户​​名:密码

where the row of x would represent the base64 encoded username:password.

然而,在支持高科技交谈,他们使用的实际执行情况是:

However, talking with the support tech, the actual implementation they use is:

Authorization: username keyid

没有base64编码,没有基本的关键字。用户名甚至没有成为你的用户名

No base64 encoding, no Basic keyword. Username doesn't even have to be your username.

所以,这里是工作的代码:

So, here is the working code:

using(var http = new HttpClient())
{
   http.DefaultRequestHeaders.Authorization = 
        new AuthenticationHeaderValue("Basic", mailchimpapikey-us1);
   string content = await http.GetStringAsync(@"https://us1.api.mailchimp.com/3.0/lists");
   Console.WriteLine(content);
}

修改
注意的意见。 TooMuchPete是在正常的HTTP基本认证头做的工作是正确的。显然,我打在边MailChimp一些旧代码或东西。

EDIT Note the comments. TooMuchPete was correct in that the normal HTTP Basic Auth headers do work. Apparently I was hitting some old code or something on the MailChimp side.

我要离开岗位作为的人谁试图调用新的3.0 API的参考。

I'm leaving the post as a reference for anyone who is trying to call the new 3.0 API.

这篇关于调用与.net MailChimp API V3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆