我正在开发 Spring MVC + Apache Oltu + Twitter 示例。在此示例中,我通过在此处注册/创建应用程序,从链接https://apps.twitter.com/app/new创建了client Id and Secrete
。现在在ClientId和Secrete的帮助下,我想调用Twitter的REST API。我也按照该链接获取了scope
https://developers.artsy.net/docs/authentication。
我在我的应用程序中使用了以下参数:
AUTHORIZATION_URL = "https://api.twitter.com/oauth/authorize";
CLIENT_ID = "RSvE3qVXXXXXXXXXXXXXXXX";
CLIENT_SECRET = "nKEWRoOcXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
REDIRECT_URL = "http://localhost:8080/apache-oltu/twitter/redirect";
ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
clientId
和secrete
出于安全目的,我未在此处正确显示。现在,当我打电话给我的控制器时,我发现Twitter有以下错误。
我的控制器实际上是重定向到:
https://api.twitter.com/oauth/authorize?scope=offline_access&response_type=code&redirect_uri=
http%3A%2F%2Flocalhost%3A8080%2Fapache-oltu%2Ftwitter%2Fredirect&client_id=RSvE3qV6Hxfdgv39RJIjDvhhc
请指导/帮助我这里出了什么问题。有什么帮助吗?
scope=offline_access
是否正确?我已经从我的
controller:
@Controller
@RequestMapping("/twitter")
public class TwitterController {
private static final Logger LOGGER = LoggerFactory.getLogger(TwitterController.class);
private static final String AUTHORIZATION_URL = "https://api.twitter.com/oauth/authorize";
private static final String CLIENT_ID = "RSvE3qVXXXXXXXXXXXXXXXXXXXXXX";
private static final String CLIENT_SECRET = "nKEWRoXXXXXXXXXXXXXXXXXXXXXXXXXX";
private static final String REDIRECT_URL = "http://localhost:8080/apache-oltu/twitter/redirect";
private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
@RequestMapping(value = "/auth", method = RequestMethod.GET)
public String authenticate() throws OAuthSystemException {
OAuthClientRequest request = OAuthClientRequest
.authorizationLocation(AUTHORIZATION_URL)
.setClientId(CLIENT_ID)
.setRedirectURI(REDIRECT_URL)
.setResponseType("code")
.setScope("offline_access")
.buildQueryMessage();
System.out.println("REDIRECT TO: "+request.getLocationUri());
return "redirect:" + request.getLocationUri();
}
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public HttpEntity<String> redirect(@RequestParam(value = "code", required = false) String code) throws OAuthSystemException, OAuthProblemException {
String value = "UNKNOWN";
if (code != null && code.length() > 0) {
System.out.println("Received CODE: "+code);
getAccessToken(code);
value = code;
}
return new ResponseEntity<String>(value,HttpStatus.OK);
}
..........................
.........................
}
任何帮助?
答案 0 :(得分:0)
请参阅此文档。 https://dev.twitter.com/web/sign-in/implementing
我认为您需要在调用授权网址(https://api.twitter.com/oauth/request_token)之前调用请求令牌网址(https://api.twitter.com/oauth/authorize)以获取oauth_token。
这是额外的步骤和其他OAuth提供商......