0

I'm trying to access data through an API call. The authentication requirement is:

The authentication token needs to be passed as a header in the calls under the header name ACCESS-TOKEN. NOTE: Add Content-Type as Application/JSON

var uri = new Uri(string.Format("https://stg-api.oyorooms.ms/api/v2/hotels/get_availability/"));
var json = JsonConvert.SerializeObject(action);
var content = new StringContent(json, Encoding.UTF8, "application/json");

HttpResponseMessage response = null;
if (isNewItem)
{
    // client.BaseAddress = new Uri("https://stg-api.oyorooms.ms/api/v2/hotels/get_availability/");
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("ACCESS-TOKEN", "c19.....j5XeS0=");
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");
    request.Content = new StringContent(content.ToString(),
            Encoding.UTF8,
            "application/json");
    response = client.PostAsync(uri, content).Result;

    resp = response.Content.ToString();//for getting break points
}
var resp1 = response.Content.ToString();//for getting break points

if (response.IsSuccessStatusCode)
{
    string responsedone = await response.Content.ReadAsStringAsync();
    var Items = JsonConvert.DeserializeObject<List<OyoSearchData>>(responsedone);
    var ids = Items[0].Hotels.Hotel[0].ID;
}

The response I got was:

{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: close,  Transfer-Encoding
Date: Sat, 22 Jun 2019 05:33:41 GMT
Server: nginx/1.14.1
Transfer-Encoding: chunked
X-Android-Received-Millis: 1561181624449
X-Android-Response-Source: NETWORK 401
X-Android-Selected-Protocol: http/1.1
X-Android-Sent-Millis: 1561181624232
Content-Type: application/json;charset=UTF-8
}}

Why am I getting this error? Is my access token is not being sent along with the headers? Please help.

Other code I tried was:

public async Task GetOyoApiDataCall(OyoHotelavailability action, bool isNewItem = false)
{
    string resp = null;
    try
    {

        var uri = new Uri(string.Format("https://stg-api.oyorooms.ms/api/v2/hotels/get_availability/"));

        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("ACCESS-TOKEN", "c19.......W5XeS0=");
        var json = JsonConvert.SerializeObject(action);
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        HttpResponseMessage response = null;
        if (isNewItem)
        {
            response = client.PostAsync(uri, content).Result;
            resp = response.Content.ToString();//for getting break points
        }
        var resp1 = response.Content.ToString();//for getting break points

        if (response.IsSuccessStatusCode)
        {
            string responsedone = await response.Content.ReadAsStringAsync();
            var Items = JsonConvert.DeserializeObject<List<OyoSearchData>>(responsedone);
            var ids = Items[0].Hotels.Hotel[0].ID;
        }
    }
    catch (Exception e)
    {
    }
}

Getting same error.

entpnerd
  • 10,049
  • 8
  • 47
  • 68
Almas
  • 17
  • 6

1 Answers1

0

Your problem is that based on your code, you aren't actually setting a HTTP header of the form:

ACCESS-TOKEN: c19.......W5XeS0=

Based on the line of code client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("ACCESS-TOKEN", "c19.......W5XeS0=");, what you're doing is creating a header of the form:

Authorization: ACCESS-TOKEN c19.......W5XeS0=

Instead, you need to add a custom header called ACCESS-TOKEN. There is a nice answer on the subject by @LibinJoseph. Basically, you just need to substitute your problematic line with a line of the form:

client.DefaultRequestHeaders.Add("ACCESS-TOKEN","c19.......W5XeS0=");

I hope that helps!

entpnerd
  • 10,049
  • 8
  • 47
  • 68
  • If it worked, please consider accepting the answer. :-) – entpnerd Jun 22 '19 at 06:59
  • Sounds like another question to ask if you here on SO if you can't figure it out, but definitely a separate issue. :-) – entpnerd Jun 22 '19 at 07:19
  • client.DefaultRequestHeaders.Add("ACCESS-TOKEN", "cURTM..t4QWQ="); response = client.PostAsync(uri, content).Result; unauthorised error is getting in this case,but working perfectly in var response = await client.GetAsync(uri); – Almas Jun 24 '19 at 08:43
  • why not getting in PostAsync(uri, content).please help anyone. – Almas Jun 24 '19 at 09:18
  • For this last comment, I'm not sure, but I would suggest that you ask a new separate question, as that will get more attention. Likely nobody besides me will see your comment above. – entpnerd Jun 25 '19 at 14:19