如何将标题添加到RESTTEMPLATE中
我正在尝试将标头添加到RESTTEMPLATE中,其中一种方法Exchange
。 在标题中,我正在放置令牌访问,我们可以访问它。 我遇到的错误是我未获得授权,401状态正在回馈。有什么建议我做错了什么?
HttpHeaders headersAuth = new HttpHeaders();
headersAuth.set(HttpHeaders.ACCEPT,MediaType.APPLICATION_JSON_VALUE);
HttpEntity<?> entityAuth = new HttpEntity<>(headersAuth);
String urlTemplateAuth = UriComponentsBuilder.fromHttpUrl("some url")
.encode()
.toUriString();
Map<String,String> queryParamsAuth = new HashMap<>();
queryParamsAuth.put("Authorization",tokenValue); //here is my token access
ResponseEntity <UserGetPhoneDto> userGetPhoneDtoResponseEntity = restTemplate.exchange(urlTemplateAuth,HttpMethod.GET,entityAuth,UserGetPhoneDto.class,queryParamsAuth);
//here i am getting error of 401 status
I am trying to add a header into the restTemplate, with one of its methods exchange
.
In header i am putting the token access, which we can access with.
The error i am getting is that i am not Authorized, 401 status is giving back . Any advices what i am doing wrong?
HttpHeaders headersAuth = new HttpHeaders();
headersAuth.set(HttpHeaders.ACCEPT,MediaType.APPLICATION_JSON_VALUE);
HttpEntity<?> entityAuth = new HttpEntity<>(headersAuth);
String urlTemplateAuth = UriComponentsBuilder.fromHttpUrl("some url")
.encode()
.toUriString();
Map<String,String> queryParamsAuth = new HashMap<>();
queryParamsAuth.put("Authorization",tokenValue); //here is my token access
ResponseEntity <UserGetPhoneDto> userGetPhoneDtoResponseEntity = restTemplate.exchange(urlTemplateAuth,HttpMethod.GET,entityAuth,UserGetPhoneDto.class,queryParamsAuth);
//here i am getting error of 401 status
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尚未将令牌设置为标题,而是将其设置在查询参数中。
您可以使用
HeaderSauth.setBearerauth()
设置携带者令牌,或使用setBasicauth()
设置用户名和密码。将其放在标题中的另一种方法:
OAuth2服务器也可以在查询参数中检索您的令牌,并使用名称为“ Access_Token”。
You have not set token to header yet, you set it in your query parameter.
You can use
headersAuth.setBearerAuth()
to set bearer token, or usesetBasicAuth()
to set username and password.Another way to put it in your header:
OAuth2 server can retrieve your token in query parameter with name 'access_token' too.
您的代码不会将令牌放入请求标题中。
您可以阅读此文档以了解有关HTTP的更多信息。
https://developer.mozilla.mozilla.org/en-us/us/docs/web/web/ http
Your code doesn't put the token into the request header.
You can read this document to learn more about http.
https://developer.mozilla.org/en-US/docs/Web/HTTP