private Credential refreshAccessToken() throws IOException { HttpTransport transport = new NetHttpTransport(); JacksonFactory jsonFactory = new JacksonFactory(); TokenResponse response = new TokenResponse(); try { response = new GoogleRefreshTokenRequest( transport, jsonFactory, REFRESH_TOKEN, CLIENT_ID, CLIENT_SECRET) .execute(); } catch (TokenResponseException e) { if (e.getDetails() != null) { System.err.println("Error: " + e.getDetails().getError()); if (e.getDetails().getErrorDescription() != null) { System.err.println(e.getDetails().getErrorDescription()); } if (e.getDetails().getErrorUri() != null) { System.err.println(e.getDetails().getErrorUri()); } } else { System.err.println(e.getMessage()); } } return new GoogleCredential.Builder() .setClientSecrets(CLIENT_ID, CLIENT_SECRET) .setJsonFactory(jsonFactory) .setTransport(transport) .build() .setAccessToken(response.getAccessToken()); }
public static HttpRequestFactory createRequestFactoryWithAccessTokenOnly(String accessToken) { TokenResponse tokenResponse = new TokenResponse(); tokenResponse.setAccessToken(accessToken); return HTTP_TRANSPORT.createRequestFactory( new GoogleCredential().setFromTokenResponse(tokenResponse)); }
@Override public void run() throws SmartServiceException { AuthorizationCodeFlow flow; TokenResponse tokenResponse; try { flow = AuthUtil.newAuthorizationCodeFlow(); tokenResponse = flow.newTokenRequest(authCode) .setRedirectUri(AppianGlasswareUtils.getAuthCallbackUri()) .execute(); // Extract the Google User ID from the ID token in the auth response String userId = ((GoogleTokenResponse) tokenResponse).parseIdToken().getPayload().getUserId(); LOG.info("Code exchange worked. User " + userId + " logged in."); flow.createAndStoreCredential(tokenResponse, userId); // The dance is done. Do our bootstrapping stuff for this user NewUserBootstrapper.bootstrapNewUser(userId); int a = 0; updatedAuthData = new GlassAuthType(); updatedAuthData.setClientId(flow.getClientId()); updatedAuthData.setAppianUserId(authData.getUserId()); updatedAuthData.setAccessToken(tokenResponse.getAccessToken()); updatedAuthData.setRefreshToken(tokenResponse.getRefreshToken()); updatedAuthData.setUserId(userId); } catch (IOException e) { LOG.error(e); throw createException(e, USER_EXECUTION_ERROR, USER_EXECUTION_ERROR, null); } }