在头部添加授权

13 浏览
0 Comments

在头部添加授权

我有以下代码:

...
AuthenticationHeaderValue authHeaders = new AuthenticationHeaderValue("OAuth2", Contract.AccessToken);
string result = await PostRequest.AuthenticatedGetData(fullUrl, null, authHeaders);
return result; 
...
public static async Task 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;
}

response = await部分只是继续一个进行中的循环,没有发生任何事情。有什么想法我做错了什么?

问题真正是,如何发送以下头部:

Authorization: OAuth2 ACCESS_TOKEN

到外部web api。

0