我对restsharp是全新的,对c#也有些陌生,我正在尝试使用restsharp来访问一个api。
有了powershell,我就可以使用。
$ClientCredsPlainText = $ClientID + ":" + $ClientSecret
$ClientCredsBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($ClientCredsPlainText))
$AuthHeader = @{"Content-Type" = 'application/x-www-form-urlencoded'
"accept" = 'application/json'
"authorization" = "Basic $ClientCredsBase64" }
$AuthAPI = @{"endpoint" = 'https://foo.bar.com/api/'
"url" = '/now/table/cmdb_ci_server?u_owner_group=' }
$AuthBody = @{"grant_type" = 'password'
"username" = $SessionUser
"password" = $SessionPassword }
$finalresult=@()
$mdsownergroup=@('foo','bar','foofoo','barbar')
foreach ($item in $mdsownergroup) {
$cmdburi = $AuthAPI.endpoint + $AuthAPI.url + $item
$cmdbrequest = Invoke-RestMethod -Method GET -Uri $cmdburi -Headers $AuthHeader -Body $AuthBody
$finalresult+=$cmdbrequest.result
}
$cmdbrequest.result
我正在用restsharp和postman在c#中尝试同样的事情,并建议使用以下方法。
var client = new RestClient("https://foo.bar.com/api/");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("X-IBM-Client-Id", "xxxxxx");
request.AddHeader("Authorization", "[{\"key\":\"\",\"value\":\"xxxxxx",\"description\":\"\",\"type\":\"text\",\"enabled\":true}]");
request.AddHeader("Authorization", "Basic xxxx");
request.AddHeader("Cookie", "JSESSIONID=xxxxx glide_user_route=glide.xxxx glide_session_store=xxxx; BIGipServerpool_foo=209737994.35390.0000");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
使用这个,我得到了:
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Authentication failed because the remote party has closed the transport stream.
发布于 2020-07-30 04:04:54
实际上,在与另一个开发人员审阅后,我发现我需要使用基本身份验证并删除用户名和密码。也就是说,通过同时传递客户端id和密钥以及用户名和密码,I会变得非常复杂。
https://stackoverflow.com/questions/63157562
复制相似问题