我正在尝试通过API调用访问数据。身份验证要求是:
身份验证令牌需要在标题名称ACCESS-TOKEN下作为调用中的标题传递。 注意:将Content-Type添加为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;
}
我得到的答复是:
{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
}}
为什么会出现此错误?我的访问令牌没有与标题一起发送吗?请帮忙。
我尝试过的其他代码是:
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)
{
}
}
遇到相同的错误。
答案 0 :(得分:0)
您的问题是,根据您的代码,您实际上并未设置以下形式的HTTP标头:
访问令牌:c19 ....... W5XeS0 =
根据代码行client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("ACCESS-TOKEN", "c19.......W5XeS0=");
,您正在做的是创建表单的标题:
授权:ACCESS-TOKEN c19 ....... W5XeS0 =
相反,您需要添加一个名为ACCESS-TOKEN
的自定义标头。 @LibinJoseph在这个主题上有一个nice answer。基本上,您只需要用以下形式的行替换有问题的行:
client.DefaultRequestHeaders.Add("ACCESS-TOKEN","c19.......W5XeS0=");
希望对您有帮助!