3

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

Junaid
  • 609
  • 1
  • 10
  • 27

2 Answers2

3

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

MvdD
  • 22,082
  • 8
  • 65
  • 93
  • Install Fiddler and check the Authorization header that goes over the line. It should be in the format: "Basic"Base64(username:password) – MvdD Jul 11 '15 at 15:43
  • 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 Jul 11 '15 at 16:36
  • 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. – Junaid Jul 11 '15 at 16:49
  • In Fiddler under tools there's a text wizard. You can use it to decode the base64 string and see what is different. – MvdD Jul 11 '15 at 16:50
0

You need to first obtain access token from Sabre that you can later use while making rest api calls. The access token POST request looks like this:

POST https://api.test.sabre.com/v2/auth/token 
Authorization: Basic ZVc5MWNtTnNhV1Z1ZEdsazplVzkxY21Oc2FXVnVkSE5sWTNKbGRBPT0=
Content-Type: application/x-www-form-urlencoded 
grant_type=client_credentials

where the value of Authorization after Basic is the Base64 encoded string based on your clientId and secret Refer to Sabre Authentication on how this string is created

So, in order to get the access token you just need to send a POST request with required header and request parameters and you do not need to use the Authenticator

amishra
  • 951
  • 9
  • 12
  • 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 Jul 11 '15 at 15:58