0

我通过浏览器 Javascript 获取请求令牌。

下面的 Java 代码可以用它来换取访问令牌。

import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;

GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY, clientId, clientSecret, authCode, "postmessage").execute();

我的问题是谷歌图书馆调用带有相当大的足迹。

因此,我尝试了 scribe(Kobee1203 oauth 2.0 fork)。它对我不起作用:

  OAuthService service = new ServiceBuilder()
                                .provider(GoogleApi20.class)
                                .apiKey(apiKey)
                                .apiSecret(apiSecret)
                                .scope(SCOPE)
                                .grantType(OAuthConstants.GRANT_TYPE_AUTHORIZATION_CODE)
                                .accessType("offline")
                                .build();
  Verifier verifier = new Verifier(authcode);
  Token accessToken = service.getAccessToken(null, verifier);

谷歌响应和错误抱怨和间接重定向 url。api 控制台中没有设置重定向 url,并且没有之前的代码也可以正常工作。

我想要一个基于 Java 的轻量级解决方案,用请求交换访问令牌以进行一次基本的授权调用。它不一定是抄写员。

4

1 回答 1

0

它只是一个 REST URL,您可以直接从您的应用程序构建和调用它。

如果您完成 Oauth Playground https://developers.google.com/oauthplayground中的步骤,您可以看到 URL 和响应。例如,要将身份验证代码转换为刷新令牌,您可以调用

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-length: 250
content-type: application/x-www-form-urlencoded
user-agent: google-oauth-playground
code=4%2FcQ7Nh2AZL3QgfynGKSloFTND3hlv.8jT4Txflqs8WXE-sT2ZLcbQTyOPFgQI&redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&client_id=407408718192.apps.googleusercontent.com&scope=&client_secret=************&grant_type=authorization_code

URL 和响应记录在https://developers.google.com/accounts/docs/OAuth2

于 2013-09-03T08:32:17.877 回答