gpt4 book ai didi

c# - C# 中的 HTTPClient 和 JWT 身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 20:53:04 25 4
gpt4 key购买 nike

我很难尝试在 C# Winforms 中使用授权。我的目的是访问 API。我已经获得了访问 token ,但是当我尝试将它传递给 header 时,它返回了一个错误:

HTTP error 400.

我的代码:

using (var httpClient = new HttpClient())
{
var url = @"https://acerportal.com/v1/414232363/status/device/";
httpClient.BaseAddress = new Uri(url);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", accessToken);

var response = await httpClient.GetStringAsync(url);
string checkResult = response.ToString();
MessageBox.Show(checkResult);
}

最佳答案

标题看起来有点不同,格式是Authorization: <type> <credentials> - 所以对于 jwt 来说是这样的:

httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

有关解释,另请参阅 https://security.stackexchange.com/questions/108662/why-is-bearer-required-before-the-token-in-authorization-header-in-a-http-re

关于c# - C# 中的 HTTPClient 和 JWT 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53273927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com