Oauth Java轻量级方式在google上交易请求访问令牌

Oauth Java轻量级方式在google上交易请求访问令牌,oauth,oauth-2.0,google-oauth,scribe,Oauth,Oauth 2.0,Google Oauth,Scribe,我通过浏览器Javascript获取请求令牌 下面的Java代码使用它来交换访问令牌 import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; GoogleTokenResponse tokenResponse = new Goog

我通过浏览器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();
我的问题是google library的调用占用了相当大的空间

因此,我尝试了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的解决方案,用请求交换访问令牌来进行一次基本的授权调用。它不一定是scribe。

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

如果您在Oauth操场中完成这些步骤,您可以看到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和响应记录在

是的,我已经在几个不同的HTTP库中构建了它,而且做起来并不复杂。好吧,原来对我来说,秘诀是a)将神奇的值“postmessage”设置为重定向uri,b)确保根本不设置任何作用域。