restsharp获取错误(身份验证)

时间:2018-06-19 14:44:47

标签: authentication get restsharp

我有一个API GET调用,该调用可在邮递员中使用,但不适用于VS2017。我复制了代码 来自邮递员:

var client = new RestClient("http://server-    d01:9000/amp/portal/api/dougtest/v1");
var request = new RestRequest(Method.GET);
request.AddHeader("Postman-Token", "19763da3-4b00-4e92-83e0-1ac75f99d219");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("X-XSRF-TOKEN", "71cf12361-8090-499a-adc3-2d5e98a04143");
request.AddParameter("undefined", "{\n    \"username\":\"domain\\\\username\",\n    \"password\":\"myPasswd\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

我得到的错误是未经授权的,API需要标头X-XSRF-TOKEN,标头值为71cf12361-8090-499a-adc3-2d5e98a04143(假密钥)

使用邮递员,一切正常,但是使用VS2017时出现错误:

  

{"status":"unauthorized","error":"Login is required","errormsg":null}

奇怪的是(我是新来的,请原谅我的无知)是标题不包含:

request.AddHeader("X-XSRF-TOKEN", "71cf12361-8090-499a-adc3-2d5e98a04143");

但是参数确实可以,这很令人困惑,因为我认为AddHeader会将其添加到标题中,但是也许我误会了某些东西。...

任何建议/建议都将受到欢迎

提前谢谢

dougc

1 个答案:

答案 0 :(得分:0)

请使用以下代码并检查

var client = new RestClient("http://server-    d01:9000/amp/portal/api/dougtest/v1");
var request = new RestRequest(Method.GET);
//Make sure to give the Valid Token
request.AddHeader("Authorization","X-XSRF-TOKEN <<Generated Token>>");
IRestResponse response = client.Execute(request);

Console.WriteLine("Response :" + response.Content);