无法在Chargify REST API上进行身份验证

问题描述:

我试图对Chargify API上的简单GET请求进行身份验证。我可能错过了关于如何将凭证传递给服务的细节。无法在Chargify REST API上进行身份验证

我得到了以下错误:

"The underlying connection was closed: An unexpected error occurred on a send."

"Authentication failed because the remote party has closed the transport stream."

我使用的代码如下:

const string url = "https://subdomain.chargify.com/subscriptions.json"; 
var request = (HttpWebRequest)WebRequest.Create(url); 

string auth = Convert.ToBase64String(Encoding.ASCII.GetBytes("apikey:x")); 
request.Headers[HttpRequestHeader.Authorization] = "Basic " + auth; 

WebResponse response = request.GetResponse(); //THROW ERROR 

using (Stream responseStream = response.GetResponseStream()) 
{ 
    var reader = new StreamReader(responseStream, Encoding.UTF8); 
    return reader.ReadToEnd(); 
} 

我下面的Chargify API文档中的说明,您可以在这里咨询信息:

https://docs.chargify.com/api-authentication

https://docs.chargify.com/api-introduction

使用chrome扩展“高级REST客户端”,我使用上面的url做了一个GET请求,然后chrome在传统弹出窗口中请求凭证,我把我的api密钥和'x'作为密码,然后我在json中得到一个我期待的答案。所以我知道API密钥是正确的,我错过的是如何通过请求在C#中传递信息。

在文档中使用curl,他们举这个例子:

你有任何想法,如何在这袅袅-u参数应在C#中被翻译?

谢谢!

编辑 正如我使用.NET包装试过评论建议,但我得到确切的错误。这里是我的代码示例:

var _apiConnection = new ChargifyConnect(url, _userName, _password); 
var productList = _apiConnection.GetProductList(); 
+1

请查看以下https://docs.chargify.com/api-code下的[Documentation](http://chargify.codeplex.com/documentation ) – MethodMan

+0

我做了,使用.NET包装我得到了同样的错误。我在这个问题中使用这个包装器添加了代码示例。 – jmecwel

+0

@jmecwel看到我的评论关于下面的包装 – djbyter

由于连接刚刚关闭,没有回复,这可能与本周生效的TLS 1.2要求有关。

https://docs.chargify.com/tls-upgrade-notice

+0

真棒!事实上,在创建我的请求之前添加以下内容:ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; – jmecwel

+0

实际上,您可以将'ChargifyConnect'上的'ProtocolType'属性设置为相同的值 - SecurityProtocolType.Tls12。它做同样的事情,它只是在实际打开连接之前调用它,所以没有地方让它切换回来。 – djbyter

+0

@djbyter谢谢,很高兴知道我可以使用包装,它可以节省很多工作! – jmecwel

我利用TLS版本明确设置为1.2的固定这一点。在您拨打Chargify之前只需添加以下代码:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12