java Spring RestTemplate 在从 Twitter 获取数据时提供 401 Authorization Required

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35979345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:50:07  来源:igfitidea点击:

Spring RestTemplate gives 401 Authorization Required while getting data from Twitter

javaspringresttemplate

提问by Kiran Pariyar

I am trying to get twitter tweets using Spring RestTemplate. My code is below:

我正在尝试使用 Spring RestTemplate 获取 twitter 推文。我的代码如下:

    public static void main(String[] args) {

    RestTemplate restTemplate = new RestTemplate();

    org.springframework.http.HttpHeaders httpHeaders = new org.springframework.http.HttpHeaders();

    String url = "https://api.twitter.com/1.1/search/tweets.json q=java";        
    String headerName = "Authorization";
    String headerValue = OAUTH_PARAMETERS
    httpHeaders.add(headerName, headerValue);
    httpHeaders.add("Content-Type", "application/json");
    HttpEntity<String> requestEntity = new HttpEntity<>("Headers", httpHeaders);
    System.out.println(">>>>>>>" + restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class).getBody());
}

I am getting following errors:

我收到以下错误:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Authorization Required at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475) at com.lftechnology.amlf.screening.service.impl.BalanceMediaSearchResultServiceImpl.main(BalanceMediaSearchResultServiceImpl.java:202)

线程“main”中的异常 org.springframework.web.client.HttpClientErrorException: 401 Authorization Required at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) at org.springframework.web.client.RestTemplate.handleResponse (RestTemplate.java:641) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557) at org.springframework。 web.client.RestTemplate.exchange(RestTemplate.java:475) 在 com.lftechnology.amlf.screening.service.impl.BalanceMediaSearchResultServiceImpl.main(BalanceMediaSearchResultServiceImpl.java:202)

回答by Mukul Tripathi

It appears that you are setting wrong parameters in the header. Try this:

看来您在标题中设置了错误的参数。试试这个:

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

notice that the space after Bearer is important.

注意 Bearer 后面的空格很重要。