@Override public GoogleTokenResponse execute() throws IOException { return executeUnparsed().parseAs(GoogleTokenResponse.class); }
/** * @param transport HTTP transport * @param jsonFactory JSON factory * @param refreshToken refresh token issued to the client * @param clientId client identifier issued to the client during the registration process * @param clientSecret client secret */ public GoogleRefreshTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String refreshToken, String clientId, String clientSecret) { super(transport, jsonFactory, new GenericUrl(GoogleOAuthConstants.TOKEN_SERVER_URL), refreshToken); setClientAuthentication(new ClientParametersAuthentication(clientId, clientSecret)); }
/** * Refresh existing access token. Parameter must have attached refreshToken. New refreshed accessToken will be updated to this * instance of accessTokenContext * * @param accessTokenContext with refreshToken attached */ public void refreshToken(GoogleAccessTokenContext accessTokenContext) { GoogleTokenResponse tokenData = accessTokenContext.getTokenData(); if (tokenData.getRefreshToken() == null) { throw new SocialException(SocialExceptionCode.GOOGLE_ERROR, "Given GoogleTokenResponse does not contain refreshToken"); } try { GoogleRefreshTokenRequest refreshTokenRequest = new GoogleRefreshTokenRequest(TRANSPORT, JSON_FACTORY, tokenData.getRefreshToken(), this.clientID, this.clientSecret); GoogleTokenResponse refreshed = refreshTokenRequest.execute(); // Update only 'accessToken' with new value tokenData.setAccessToken(refreshed.getAccessToken()); if (log.isTraceEnabled()) { log.trace("AccessToken refreshed successfully with value " + refreshed.getAccessToken()); } } catch (IOException ioe) { throw new SocialException(SocialExceptionCode.GOOGLE_ERROR, ioe.getMessage(), ioe); } }
/** * Obtain new access token from the refresh token & client credentials. */ private String getAccessToken() { try { final TokenResponse response = new GoogleRefreshTokenRequest(new NetHttpTransport(), new JacksonFactory(), passwordRegistry.getPassword(getRefreshTokenKeyStoreId()).getDecryptedValue(), getClientId(), passwordRegistry.getPassword(getClientSecretKeyStoreId()).getDecryptedValue()).execute(); return response.getAccessToken(); } catch (IOException e) { log.debug("Error while requesting the access token.", e); if (e instanceof TokenResponseException) { TokenErrorResponse details = ((TokenResponseException) e).getDetails(); if (details != null) { log.debug(details.getError()); } } throw new RuntimeException(e); } }