我有一个服务调用,它给我一个包含一些数据的JSON响应。JSON中的一个字段是URL。此URL可用于调用另一个服务,即GET call。URL有3个查询参数,并且不需要按照指导原则进行单独的身份验证,因为从父调用中,提供者将提供一个令牌将在某个时间内有效,并且它将在URL中传递。
URL : https://ep9.abc/v2/media/files/b9fe8e90?
tokencreator=BFS&tokenexpires=1656712486&token=w8P3mbv
tokencreator: BFS
tokenexpires: 1656712486
token: w8P3mbv当我简单地点击上面的URL,它下载所有所需的文件在铬。但是当我尝试用Resttemplate在Spring调用它时,它给了我401。
我的第一个服务调用(获取这个URL)是使用OAuth 2.0。
当我看到浏览器网络选项卡下面是我看到的细节。
Request URL: https://ep9.abc/v2/media/files/b9fe8e90?
tokencreator=BFS&tokenexpires=1656712486&token=w8P3mbv
Request Method: GET
Status Code: 200 OK
Remote Address: 199.888.888.888:443
Referrer Policy: strict-origin-when-cross-origin
Accept: html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,
application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Host: ep9.abc
sec-ch-ua: ".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1下面是我试图调用resttemplate的代码,我试图提取查询params,并试图显式地发送它,但它仍然是401。
Map<String, String> params = new HashMap<>();
params.put("tokencreator", tokencreator);
params.put("tokenexpires", tokenexpires);
params.put("token", token);
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer "+token);
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<byte[]> zipFiles =
restTemplate.exchange(baseUrl,HttpMethod.GET,entity,byte[].class,params);请帮我把这个修好。
发布于 2022-07-01 23:41:31
如果我正确理解了您的问题,您应该发送一个请求,其中您发送令牌、令牌创建者和令牌过期作为查询参数。
您编写的代码没有将这些查询参数添加到基本url。为了使其工作,可以使用以下代码发送查询参数:
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer "+token);
HttpEntity entity = new HttpEntity(headers);
// Query parameters
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUrl)
// Add query parameter
.queryParam("tokencreator", tokencreator)
.queryParam("tokenexpires", tokenexpires)
.queryParam("token", token);
ResponseEntity<byte[]> zipFiles =
restTemplate.exchange(builder.build().toUri(), HttpMethod.GET,entity,byte[].class);发布于 2022-07-25 15:47:47
当我从Resttemplate转换为HttpURLConnection时,它就开始工作了
https://stackoverflow.com/questions/72835343
复制相似问题