问题
I need to create an application with Mendeley in Java. But I have problems with the oauth2's conexion.
I use Apache Oltu, but if you know another better alternative, told me please.
I have this:
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.
Thanks a lot.
回答1:
There is some documentation on our website at http://apidocs.mendeley.com/home/authentication
I've thrown together a more complete example using the Apache Oltu library with Apache HTTP Client library. This uses the anonymous access token.
Edit
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");
来源:https://stackoverflow.com/questions/23545198/oauth-2-in-mendeley-with-java