尝试执行从 ArangoDB 检索数据的 RestSharp 请求时出现未经授权的错误

Getting Unauthorized error when trying to execute the RestSharp request that retrieves data from ArangoDB

如果我通过 Postman 发送 http 请求,它会工作并且我会得到结果。但是当我通过 RestSharp 执行时,同样的方法不起作用并获得未经授权。

下面是代码片段:

var client = new RestClient(
   "http://Username:Password@localhost:port/_db/databaseName/_api/simple/all");
var request = new RestRequest(Method.PUT);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", 
     "{\n    \"collection\":\"collectionName\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response;

根据 the restsharp wiki,您无法通过 URL:

指定身份验证参数
var client = new RestClient("http://example.com");
client.Authenticator = new SimpleAuthenticator("username", "foo", "password", "bar");
var request = new RestRequest("resource", Method.GET);
client.Execute(request);

一般来说,在这种情况下删除 https 并使用 wireshark or ngrep 检查网络上发生的事情是个好主意:

GET /_db/_system/_api/version?details=true HTTP/1.1
Host: 127.0.0.1
Connection: Keep-Alive
User-Agent: ArangoDB
Accept-Encoding: deflate
Authrization: Basic cm9vdDo=

检查实际生成的身份验证 headers。

var client = new RestClient("http://example.com");
client.Authenticator = new SimpleAuthenticator("admin","admin");
var request = new RestRequest(Method.GET);
client.Execute(request);

这对我有用..