0

I do code request with string:

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

I receive oauth2 code. I do token request code based with java client.

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

And as result i receive 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"
}

There is no errors, because of correct access_token. The problem is that TokenResponse doesn't contain refresh_token data.

How could I get a refresh_token?

Sergii
  • 7,044
  • 14
  • 58
  • 116

1 Answers1

6

The refresh token send just in first time you authorize the app and then you need to save it some where . So you have to delete first the authorize you give to your app from here and then try again .

You can get more details in this question : Not receiving Google OAuth refresh token

Community
  • 1
  • 1
OriEng
  • 1,424
  • 18
  • 34
  • 2
    following doc: `refresh_token` _A token that you can use to obtain a new access token. Refresh tokens are valid until the user revokes access. Again, this field is only present in this response if you set the `access_type` parameter to `offline` in the initial request to Google's authorization server_. – Sergii Feb 27 '17 at 13:35
  • ..trying to delete first authorize – Sergii Feb 27 '17 at 13:36
  • Exactly . In your request you set access_type="offline" so the request is OK. You try to revokes the access to your app and ask again for token ? You need to save this refresh token to use him to get another access token because the access token is valid just for hour. – OriEng Feb 27 '17 at 13:38