添加授权报头 [英] Adding authorization to the headers
本文介绍了添加授权报头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code:
...
AuthenticationHeaderValue authHeaders = new AuthenticationHeaderValue("OAuth2", Contract.AccessToken);
string result = await PostRequest.AuthenticatedGetData(fullUrl, null, authHeaders);
return result;
...
public static async Task<string> AuthenticatedGetData(string url, FormUrlEncodedContent data, AuthenticationHeaderValue authValue)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authValue.Parameter);
HttpResponseMessage response = await client.PostAsync(new Uri(url), data);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
响应=等待部分只是继续正在进行的循环,并没有任何反应。任何想法我做错了吗?
The response = await part just continues an ongoing loop and nothing happens. Any ideas what I am doing wrong?
真正的问题是,我该如何发送以下标题:
The question really is, how do I send the following header:
Authorization: OAuth2 ACCESS_TOKEN
到外部Web API
to an external web api
推荐答案
这行
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(authValue.Parameter);
会产生这个头值
Authorization: ACCESS_TOKEN
其中, ACCESS_TOKEN
是 authValue.Parameter
的价值。你想分配给你传递的值,而不是得到所需要的头
Where ACCESS_TOKEN
is the value of authValue.Parameter
. You want to assign the value you passed instead to get the required header
client.DefaultRequestHeaders.Authorization = authValue;
会产生
Authorization: OAuth2 ACCESS_TOKEN
这篇关于添加授权报头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文