2014-05-08 43 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