我用字符串进行代码请求:

https://accounts.google.com/o/oauth2/v2/auth?
   scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&
   state={%22externalUserId%22:%22TEST%22}&
   access_type=offline&
   include_granted_scopes=true&
   redirect_uri=http://localhost:3344/oauth2/google/callbackcode&response_type=code&
   client_id=676016849609-r53vpjccpr9kf5uvuul7h6kvek1id2oh.apps.googleusercontent.com


我收到了oauth2代码。
我做基于Java客户端的令牌请求代码。

private AuthorizationCodeFlow flow;
@PostConstruct
public void init() {
    NetHttpTransport transport = null;
    try {
        transport = GoogleNetHttpTransport.newTrustedTransport();
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        flow = new GoogleAuthorizationCodeFlow.Builder(
                transport, JSON_FACTORY, GoogleClientSecrets.load(JSON_FACTORY,
                new InputStreamReader(GoogleCalendarClient.class.getResourceAsStream("/client_secret_676016849609-r53vpjccpr9kf5uvuul7h6kvek1id2oh.apps.googleusercontent.com.json")))
                , SCOPES).build();
    } catch (IOException e) {
        e.printStackTrace();
    }
    callBackPattern = String.format(callBackPattern, provider().lowerCaseName());
}




flow.newTokenRequest(oauthCode)
                .setRedirectUri(callBackPattern)
                .execute();


结果我收到了TokenResponse

{
  "access_token": "ya29.Glv_A8Fidn8cRmQveIy0pbDIjcxssKN61X20u4zjlDYV1NbiC-QO593_isRa8Q5ngSFr-y-zICjXw1WZy4OguOh90SCyPnz0NGYpw7I4fahzH7NORQm-bbnA9Chr",
  "expires_in": 3592,
  "id_token": "eyJhbGciOOiZUzI1NiIsImtpZCI6IjgxMDkxNGZiOTk0OGYxZTQzNTdjYzg3MjY4MDg3Mjk4ZTgzNTlkMjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiaWF0IjoxNDg4MTg0NDcxLCJleHAiOjE0ODgxODgwNzEsImF0X2hhc2giOiJaVjgxejQwV0pYX3N1WkplazZnem93IiwiYXVkIjoiNjc2MDE2ODQ5NjA5LXI1M3ZwamNjcHI5a2Y1dXZ1dWw3aDZrdmVrMWlkMm9oLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTE4MTk1OTk4MjU4MDIyMzM2MDUwIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF6cCI6IjY3NjAxNjg0OTYwOS1yNTN2cGpjY3ByOWtmNXV2dXVsN2g2a3ZlazFpZDJvaC5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImVtYWlsIjoic2VyZ2lpX3ZsYXNpdWtAdWtyLm5ldCJ9.DWkcTAwOPnirsDL_ok10GHjoe0Rg0n-uYYtSfn3tXkIg-xB6taaYi2-gdNCh64-hSzgQWsdeLu7Mga1rXfGVTw-iBPWhU80MhoiH_YZtLpAK7f94rBajqHa8ucei1P7RSZxRD-RdB1YMjpJPQhKx5DV0W9xBPB3LN8s1C3vL-06Y4nu7yB2ZFllG6SIJbl7f0Kn2S_SCAxhxGvwSuIqW4ogXwqc0njdBwWlOvdxn8hQ33dftljn-Q5fJ0iEroLEhUhnGwmGAcr7yl-HRjZQvz6ICyQLvRGDv6J12pEdDu2S0mGZfV_zNLG2-EKqo6xm99WsFSMsV_6_TeCG478f7bQ",
  "token_type": "Bearer"
}


没有错误,因为正确的access_token。问题是TokenResponse不包含refresh_token数据。

如何获得refresh_token

最佳答案

刷新令牌仅在您第一次授权应用程序时发送,然后需要将其保存在某个位置。因此,您必须先从here中删除您授予应用程序的授权,然后再试一次。

您可以在此问题中获取更多详细信息:Not receiving Google OAuth refresh token

07-22 14:34