OAuth 2.0 authentication in RestSharp
Seems to me like you are adding the Authorization header twice. The documentation here says
The authenticator’s Authenticate method is the very first thing called upon calling RestClient.Execute
Looking at the implementation of HttpBasicAuthenticator, the Authenticate method adds the appropriate header to the request.
So remove the following line from your example:
request.AddHeader("Authorization", "Basic " + client);
Comments
-
Junaid almost 2 years
I am trying to authenticate RESTful service (sabre REST api) using RESTsharp library but i am not able to authenticate it. I am using my Client id and secret. Please tell me how to authenticate using oAuth 2.0 authenticator.
I have tried this code. ( sabre is using OAuth 2.0 authentication )
public ActionResult Index() { var client = new RestClient("https://api.test.sabre.com"); client.Authenticator = new HttpBasicAuthenticator("myclientid", "myclientsecret"); RestRequest request = new RestRequest("/v1/auth/token", Method.POST); request.AddHeader("Authorization", "Basic " + client); request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request.AddParameter("grant_type", "client_credentials"); IRestResponse response = client.Execute(request); var content = response.Content; ViewBag.R = content; return View(); }
i got this result
{"error":"invalid_client","error_description":"Credentials are missing or the syntax is not correct"}
please tell what i am doing wrong. Thanks
Snapshot of Fiddler Comparison of Running code (not with RestSharp) and code using RestSharp is shown
With RestSharp
-
MvdD almost 9 yearsInstall Fiddler and check the Authorization header that goes over the line. It should be in the format: "Basic"<space>Base64(username:password)
-
Junaid almost 9 yearsI successfully got the Access token using Sabre Authentication guidelines already but i wanted to implement the same thing using Rest Sharp client which is builtin library for that purpose.
-
Junaid almost 9 yearsI have checked with the Fiddler and the thing is I am getting the header in the same format you described but i think its not doing the Base64 operation because i have compared it with already running code (not with Restsharp client). I have edited my post by added the comparison snapshots of both. please take a look. thnx
-
Junaid almost 9 yearsI got the answer. Thnx for telling me to use fiddler. It really helped me. It was not doing the base64 operation on clientId and clientSecret. So I did that manually and code worked like a charm.
-
MvdD almost 9 yearsIn Fiddler under tools there's a text wizard. You can use it to decode the base64 string and see what is different.