简体   繁体   中英

Using RestSharp with Gerrit REST API

I want to use RestSharp to issue Api calls to Gerrit but I'm having trouble with authentication.

For example there is the curl command which works:

curl --digest --user VladDracul:5SAbg1pFWyqsvcs4aB7aGL2lISh8fuOjcoQK9WRGSA http://localhost:8080/a/groups/

but how can I give the --user to a restSharp call?

myAuth = new HttpBasicAuthenticator("VladDracul","5SAbg1pFWyqsvcs4aB7aGL2lISh8fuOjcoQK9WRGSA");
restClient = new RestClient(BaseUrl);
restClient.Authenticator = myAuth;

 var request = new RestRequest(Method.GET);
 request.Resource = "/a/groups/";
 request.AddHeader("Content-type", "application/json");

 var response = restClient.Execute(request);

The response I get is "Unauthorized"

I found the answer. Adding the line gets it working.

 var request = new RestRequest(Method.GET);
 request.Credentials = new NetworkCredential("VladDracul", "5SAbg1pFWyqsvcs4aB7aGL2lISh8fuOjcoQK9WRGSA");;
 request.Resource = "/a/groups/";
 request.AddHeader("Content-type", "application/json");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM