C# 使用Api Id和Api密钥的RestSharp身份验证
我必须调用restful API,但我仅有的细节是API ID和API密钥 我正试图像这样使用Restsharp库代码C# 使用Api Id和Api密钥的RestSharp身份验证,c#,api,rest,restsharp,C#,Api,Rest,Restsharp,我必须调用restful API,但我仅有的细节是API ID和API密钥 我正试图像这样使用Restsharp库代码 var client = new RestClient("https://xxxxxxx"); client.Authenticator = new HttpBasicAuthenticator("xxxxx", "yyyyyy"); 我得到一个401授权要求的错误 你能给我指一下正确的方向吗 谢谢。这是我提出的解决方案 这将返回客户机对象 private RestC
var client = new RestClient("https://xxxxxxx");
client.Authenticator = new HttpBasicAuthenticator("xxxxx", "yyyyyy");
我得到一个401授权要求的错误
你能给我指一下正确的方向吗
谢谢。这是我提出的解决方案 这将返回客户机对象
private RestClient InitializeAndGetClient()
{
var cookieJar = new CookieContainer();
var client = new RestClient("https://xxxxxxx")
{
Authenticator = new HttpBasicAuthenticator("xxIDxx", "xxKeyxx"),
CookieContainer = cookieJar
};
return client;
}
你可以使用这样的方法
var client = InitializeAndGetClient();
var request = new RestRequest("report/transaction", Method.GET);
request.AddParameter("option", "value");
//Run once to get cookie.
var response = client.Execute(request);
//Run second time to get actual data
response = client.Execute(request);
希望这对你有帮助
普拉卡什。我知道这很旧,但这是我所需要的:
var cookieJar = new CookieContainer();
var client = new RestClient("https://xxxxxxx")
{
Authenticator = new HttpBasicAuthenticator("[username]", "[password]"),
CookieContainer = cookieJar
};
在创建请求对象之后,我必须添加API_密钥头:
var request = new RestRequest("[api]/[method]"); // this will be unique to what you are connecting to
request.AddHeader("API_KEY", "[THIS IS THE API KEY]");