在从Java应用程序获取请求期间获取403错误

在从Java应用程序获取请求期间获取403错误,java,rest,spring-boot,get,Java,Rest,Spring Boot,Get,我试图使用RestTemplate库对JAVA客户端执行GET请求,导致以下错误: Exception in thread "main" org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden 当我试图通过命令行点击相同的URL时,它工作正常。在这里发布cURL命令和Java代码片段 卷曲: JAVA代码段: 问题是因为URL是HTTPS而不是HTTP吗?添加用户代理头,尝试让我们知道它是

我试图使用RestTemplate库对JAVA客户端执行GET请求,导致以下错误:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden
当我试图通过命令行点击相同的URL时,它工作正常。在这里发布cURL命令和Java代码片段

卷曲:

JAVA代码段:


问题是因为URL是HTTPS而不是HTTP吗?

添加用户代理头,尝试让我们知道它是否有效

String URL="https://xyz";
RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();

//setting up the required headers
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
//settting user agent
headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");

headers.set("Authorization", "Bearer "+accessToken);

HttpEntity<String> entity = new HttpEntity<String>("body",headers);

//get request
ResponseEntity<String> responseEntity = restTemplate.exchange(URL, HttpMethod.GET, entity, String.class);

这个问题是因为URL是HTTPS而不是HTTP吗?很可能,我们怎么知道您还没有发布您在Java中使用的URL。URL是https://xyz,由于安全原因,无法发布完整的URL。能否尝试使用-v使用curl verbose命令获取完整的请求详细信息,并尝试将其与java.Hi生成的URL匹配,现在在线程main org.springframework.web.client.HttpClientErrorException$Unauthorized:401Unauthorized中获取异常。这意味着您拥有无效的accessToken,您需要获取有效的令牌。
String URL="https://xyz";
RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();

//setting up the required headers
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.set("Authorization", "Bearer "+accessToken);

HttpEntity<String> entity = new HttpEntity<String>("body",headers);

//get request
ResponseEntity<String> responseEntity = restTemplate.exchange(URL, HttpMethod.GET, entity, String.class);
String URL="https://xyz";
RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();

//setting up the required headers
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
//settting user agent
headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");

headers.set("Authorization", "Bearer "+accessToken);

HttpEntity<String> entity = new HttpEntity<String>("body",headers);

//get request
ResponseEntity<String> responseEntity = restTemplate.exchange(URL, HttpMethod.GET, entity, String.class);