Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp - Token authentication

I'm trying to send a GET request with a token authentication, but i get an unauthorized response. If i send the same request on Postman, it works.

Here's my code :

string url = string.Format("{0}batchs", MyUrl);
RestClient client = new RestClient(url);
RestRequest getRequest = new RestRequest(Method.GET);
getRequest.AddHeader("Accept", "application/json");
getRequest.AddHeader("Authorization", "token " + MyToken);
getRequest.AddParameter("name", MyName, ParameterType.QueryString);

IRestResponse getResponse = client.Execute(getRequest);

And here's my postman request :

Postman request

Any ideas on how to correct this ?

Thanks !

like image 834
Kévin Buzit Avatar asked Mar 19 '18 08:03

Kévin Buzit


People also ask

Does RestSharp use HttpClient?

Since RestSharp uses the HttpClient, we should consider similar issues regarding the RestClient instantiation.

What is RestSharp used for?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.

Is RestSharp a framework?

RestSharp is a C# library used to build and send API requests, and interpret the responses. It is used as part of the C#Bot API testing framework to build the requests, send them to the server, and interpret the responses so assertions can be made.


1 Answers

I'm not sure exactly what kind of auth you're using, but I use a firebase token generated at runtime, and this is the only thing I could get to work for me.

request.AddHeader("authorization", "Bearer " + _fireBaseService.AuthToken);
like image 79
Swisscheese Avatar answered Oct 19 '22 20:10

Swisscheese