gpt4 book ai didi

java - 使用 Java 实现 Mendeley 中的 Oauth 2

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:26 27 4
gpt4 key购买 nike

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

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

非常感谢。

最佳答案

我们的网站上有一些文档:http://apidocs.mendeley.com/home/authentication

我使用 Apache Oltu 库和 Apache HTTP 客户端库构建了一个更完整的示例。这使用匿名访问 token 。

编辑

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

关于java - 使用 Java 实现 Mendeley 中的 Oauth 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23545198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com