带有Restsharp的RestCall未通过身份验证,与邮递员一起使用

时间:2019-02-13 19:31:33

标签: c# rest http postman restsharp

我正在尝试从需要基本身份验证的REST端点获取信息。 使用邮递员,我很好,可以从电话中获取所需的信息:

GET endpoint/api/workitems?ids=20449& api-version=2.0 HTTP/1.1
Host: xxx.xxx.xxx.50:8080
Authorization: Basic ABC==,Basic ZZZ    cache-control: no-cache
Postman-Token: e6476d89-ec2b-439d-8821-88ef446a03a9

当我使用restsharp进行相同操作时,会出现未授权的错误:

var client = new RestClient("http://xxx.xxx.xxx.50:8080/endpoint/api/workitems?ids=20449& api-version=2.0");
var request = new RestRequest(Method.GET);
request.AddHeader("Postman-Token", "06ea7553-d35e-4743-a516-201d6e3b9084");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Authorization", "Basic ABC==,Basic ZZZ");
IRestResponse response = client.Execute(request);

我想念什么吗?

谢谢

1 个答案:

答案 0 :(得分:1)

最终,我发现使用restsharp进行基本身份验证的正确方法如下。

有效:

var client = new RestClient("http://xxx.xxx.xxx.50:8080/endpoint/api/workitems?ids=20449& api-version=2.0");
client.Authenticator = new HttpBasicAuthenticator(username, decodedToken);
var request = new RestRequest(Method.GET);
request.AddHeader("Postman-Token", "06ea7553-d35e-4743-a516-201d6e3b9084");
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);