OAuth的2的Mendeley与Java [英] Oauth 2 in Mendeley with Java
问题描述
我需要创建的Mendeley在Java的应用程序。但我与的oauth2的conexion问题。
I need to create an application with Mendeley in Java. But I have problems with the oauth2's conexion.
我使用Apache奥尔图,但如果你知道另外一个更好的选择,告诉我吧。
I use Apache Oltu, but if you know another better alternative, told me please.
我有这样的:
OAuthClientRequest request = OAuthClientRequest
.tokenLocation("https://api-oauth2.mendeley.com/oauth/token")
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId(CLIENT_ID)
.setClientSecret(CLIENTE_SECRET)
.setRedirectURI(REDIRECT_URI)
.setCode("code")
.setScope("all")
.buildQueryMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class);
String accessToken = oAuthResponse.getAccessToken();
String expiresIn = oAuthResponse.getExpiresIn().toString();
System.out.println("ACCESS TOKEN: " + accessToken);
System.out.println("EXPIRES IN : " + expiresIn);
但是这会产生此异常:
but this produces this exception:
Exception in thread "main" OAuthProblemException{error='invalid_request', description='Missing parameters: access_token', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
at org.apache.oltu.oauth2.common.exception.OAuthProblemException.error(OAuthProblemException.java:59).......
你知道吗?我再说一遍,如果你知道另一种选择或解决方案请帮助我。
Any idea? I repeat, if you know another alternative or solution help me please.
非常感谢。
推荐答案
有在在我们网站上的一些文件http://apidocs.mendeley.com/home/authentication
我扔在一起使用Apache奥尔图库的Apache HTTP客户端库更完整的例子。这将使用匿名访问令牌。
I've thrown together a more complete example using the Apache Oltu library with Apache HTTP Client library. This uses the anonymous access token.
的修改的
OAuthClientRequest request = OAuthClientRequest
.tokenLocation(TOKEN_URL)
.setClientId(TRUSTED_CLIENT_ID)
.setClientSecret(TRUSTED_SECRET)
.setGrantType(GrantType.CLIENT_CREDENTIALS)
.setScope("all")
.buildBodyMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse tokenResponse = oAuthClient.accessToken(
request, OAuthJSONAccessTokenResponse.class);
HttpGet httpGet = new HttpGet(CATALOG_URL);
httpGet.setHeader("Authorization", "Bearer " + tokenResponse.getAccessToken());
HttpResponse httpResponse = apacheHttpClient.execute(httpGet);
assertThat(httpResponse.getStatusLine().getStatusCode()).isEqualTo(200);
String responseAsString = EntityUtils.toString(httpResponse.getEntity());
ObjectMapper mapper = new ObjectMapper();
Document document = mapper.readValue(responseAsString, Document.class);
assertThat(document.getTitle()).isEqualTo("Identifying and recording user actions to enable automatic online assessment");
这篇关于OAuth的2的Mendeley与Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!