我試圖對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();
請查看以下https://docs.chargify.com/api-code下的[Documentation](http://chargify.codeplex.com/documentation ) – MethodMan
我做了,使用.NET包裝我得到了同樣的錯誤。我在這個問題中使用這個包裝器添加了代碼示例。 – jmecwel
@jmecwel看到我的評論關於下面的包裝 – djbyter