简体   繁体   English

我在尝试从具有 JWT 令牌的 API 发出 GET 请求时收到错误 401(未经授权)

[英]I'm getting error 401 (unauthorized) while trying to make a GET request from my API that has a JWT token

Here is my GETRequest method...这是我的 GETRequest 方法...

 HttpClient client = new HttpClient();
            var dashboardEndpoint = Helper.GetUsersurl;
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Add("Authorization", "Bearer"+Helper.userprofile.token);

            var result = await client.GetStringAsync(dashboardEndpoint);
            var UsersList = JsonConvert.DeserializeObject<AddedUsers>(result);
            //Users = new ObservableCollection<AddedUsers>(UsersList);

            Emplist.ItemsSource = UsersList.data;

        }

I've tried different method but the token isn't being sent alongside my request and therefore the API is throwing an error 401 at me.我尝试了不同的方法,但令牌没有与我的请求一起发送,因此 API 向我抛出错误 401。 Any help will be gladly appreciated please...任何帮助将不胜感激,请...

So, i later studied the pattern and also the response on postman then i realize I'm supposed to pass only the key and the value.所以,我后来研究了 postman 上的模式和响应,然后我意识到我应该只传递密钥和值。 In this context,在这种情况下,

HttpClient client = new HttpClient();
            var dashboardEndpoint = Helper.GetUsersurl;
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Add("Authorization", Helper.userprofile.token);

            var result = await client.GetStringAsync(dashboardEndpoint);
            var UsersList = JsonConvert.DeserializeObject<AddedUsers>(result);
            //Users = new ObservableCollection<AddedUsers>(UsersList);

            Emplist.ItemsSource = UsersList.data;

I only needed to pass Authorization as the key and token as the value.我只需要传递授权作为键和令牌作为值。 thanks everyone感谢大家

As @jason said, try to add a space between Bearer and the Token正如@jason 所说,尝试在BearerToken之间添加一个空格

client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Helper.userprofile.token);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我仍然会从api get请求中收到401(未经授权)错误? - Why am I still getting a 401(Unauthorized) error from api get request? 使用有效令牌获取 401 未经授权的请求错误 - Get 401 Unauthorized request error with valid token "尝试访问我的 api 平台时未找到 JWT 令牌 401 错误?" - JWT Token not found 401 error when trying to access my api plateform? 在将Hammock与Tumblr API结合使用以检索喜欢项时,为什么会出现此401未经授权的错误? - Why am I getting this 401 unauthorized error while using Hammock with the Tumblr API to retrieve likes? Express API 发布请求中出现 401 未经授权的错误 - 401 Unauthorized error in Express API post request Chef API获取401未经授权的错误Chef - Chef API getting 401 Unauthorized error chef 我正在尝试学习从 API 请求、加载和解析 Json 数据。 我收到一个值错误 - I'm trying to learn to to request, load and parse Json data from API. I'm getting a value error 向 Spotify API 发出 PUT 请求时出现 401 错误 - Getting 401 error while making a PUT request to the Spotify API 通过Intuit API访问quickBooks数据时,.NET中出现“ Unauthorized-401”错误 - Getting “Unauthorized-401” Error in .NET while accessing quickBooks data through Intuit API 在点击 NEXMO 的发布请求时,我得到了 {“type”:“UNAUTHORIZED”,“error_title”:“Unauthorized”} - I'm gettibg {“type”:“UNAUTHORIZED”,“error_title”:“Unauthorized”} while hitting post request of NEXMO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM