1

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.

thomson_matt
  • 7,473
  • 3
  • 39
  • 47
angelo087
  • 51
  • 5
  • Finally yet I did the correct code. I changed `GitHubTokenResponse ` to: `OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request,OAuthJSONAccessTokenResponse.class);` So, I taked the access_token. – angelo087 May 08 '14 at 18:58

1 Answers1

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");
  • Please add the relevant parts of that doc to your answer. Linking to external resources without including the pertinent information is not encouraged. If the URL ever changes this answer would become useless. – indivisible May 09 '14 at 11:47