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.