不确定我是否正确执行此操作,传递令牌值,然后从网络服务获取一些信息。 我编辑了这个问题,它是通过身份验证。我将把它留给将来的搜索。
private async void Data(string AUTH)
{
using (HttpClient client = new HttpClient())
{
var Tokens = Storage.access.AUTH;
var json = JsonConvert.SerializeObject(AUTH);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AUTH);
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
var response = client.GetAsync("https://any adrees.com").Result;
string content = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
var content2 = await response.Content.ReadAsStringAsync();
var Items = JsonConvert.DeserializeObject<Mensajes>(content2);
}
Debug.WriteLine(content);
}
}
答案 0 :(得分:0)
不确定您所引用的令牌类型,我的答案将基于OAuth访问令牌。
您需要创建AuthenticationHeaderValue
并将其设置为HttpClient标头。
var authHeader = new AuthenticationHeaderValue("bearer", Storage.accessToken.Token);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = authHeader;