OAuth 2.0 authentication in RestSharp

10,921

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);

Share:
10,921
Junaid
Author by

Junaid

Love Technology

Updated on June 04, 2022

Comments

  • Junaid
    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

    using RestSharp Not using RestSharp

  • MvdD
    MvdD almost 9 years
    Install Fiddler and check the Authorization header that goes over the line. It should be in the format: "Basic"<space>Base64(username:password)
  • Junaid
    Junaid almost 9 years
    I 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
    Junaid almost 9 years
    I 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
    Junaid almost 9 years
    I 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
    MvdD almost 9 years
    In Fiddler under tools there's a text wizard. You can use it to decode the base64 string and see what is different.