【问题标题】:Oauth 2 in Mendeley with Java使用 Java 的 Mendeley 中的 Oauth 2
【发布时间】:2014-05-08 14:48:09
【问题描述】:

我需要用 Java 中的 Mendeley 创建一个应用程序。但是我对 oauth2 的连接有问题。

我使用 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).......

有什么想法吗?我再说一遍,如果您知道其他替代方案或解决方案,请帮助我。

非常感谢。

【问题讨论】:

  • 最后我做了正确的代码。我将GitHubTokenResponse 更改为:OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request,OAuthJSONAccessTokenResponse.class); 所以,我拿走了access_token。

标签: java apache oauth oauth-2.0 mendeley


【解决方案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");

【讨论】:

  • 请将该文档的相关部分添加到您的答案中。不鼓励链接到外部资源而不包括相关信息。如果 URL 发生变化,这个答案将变得毫无用处。
猜你喜欢
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 2019-02-21
相关资源
最近更新 更多