2014-05-08 27 views
1

我需要用Java中的Mendeley创建一个应用程序。但是我在oauth2的conexion上遇到了问题。Oauth 2与Java的Mendeley

我使用Apache Oltu,但如果你知道另一个更好的选择,请告诉我。

我有这样的:

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); 

但这会产生此异常:

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)....... 

任何想法?我再说一遍,如果你知道另一种替代方案或解决方案能帮助我。

非常感谢。

+0

最后但我做了正确的代码。我改变了'GitHubTokenResponse'为:'OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request,OAuthJSONAccessTokenResponse.class);'所以,我采取了access_token。 – angelo087

回答

1

有一个在http://apidocs.mendeley.com/home/authentication

在我们网站上的一些文件,我扔在一起使用Apache Oltu库的Apache HTTP客户端库更完整的例子。这使用匿名访问令牌。

编辑

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"); 
+0

请将该文档的相关部分添加到您的答案中。不鼓励与不包括相关信息的外部资源链接。如果URL改变了,这个答案将变得毫无用处。 – indivisible