2016-01-13 34 views
2

我試圖對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

回答

1

由於連接剛剛關閉,沒有回覆,這可能與本週生效的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

0

我利用TLS版本明確設置爲1.2的固定這一點。在您撥打Chargify之前只需添加以下代碼:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12