List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleRefreshTokenRequest GoogleRefreshTokenRequest
public GoogleRefreshTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String refreshToken,
String clientId, String clientSecret)
From source file:com.compguide.web.google.calendar.api.GoogleCalendar.java
public static Credential refreshToken(User user, Credential oldCredential) throws IOException { InputStream in = GoogleCalendar.class.getResourceAsStream("/client_id.json"); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); TokenResponse response = new GoogleRefreshTokenRequest(HTTP_TRANSPORT, JSON_FACTORY, oldCredential.getRefreshToken(), clientSecrets.getDetails().getClientId(), clientSecrets.getDetails().getClientSecret()).execute(); // Build flow and trigger user authorization request. GoogleAuthorizationCodeFlow flow = authorizationCodeFlow("online"); //Now, with the token and user id, we have credentials Credential credential = flow.createAndStoreCredential(response, user.getIduser().toString()); return credential; }
From source file:com.dotosoft.dotoquiz.tools.thirdparty.GoogleOAuth.java
License:Apache License
private static Credential getRefreshedCredentials(String refreshCode, Settings setting) throws IOException, GeneralSecurityException { // HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); log.info("Getting access token for refresh token.."); try {/* w w w .j ava 2s . c o m*/ if (setting.getApi().getClientSecret().getClientId().startsWith("Enter") || setting.getApi().getClientSecret().getClientSecret().startsWith("Enter ")) { System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ " + "into oauth2-cmdline-sample/src/main/resources/client_secrets.json"); System.exit(1); } GoogleTokenResponse response = new GoogleRefreshTokenRequest(httpTransport, JSON_FACTORY, refreshCode, setting.getApi().getClientSecret().getClientId(), setting.getApi().getClientSecret().getClientSecret()).execute(); return new GoogleCredential().setAccessToken(response.getAccessToken()); } catch (UnknownHostException ex) { log.error("Unknown host. No web access?"); throw ex; } catch (IOException e) { log.error("Exception getting refreshed auth: ", e); } return null; }
From source file:com.github.jdavisonc.camel.gdrive.GDriveEndpoint.java
License:Apache License
public GoogleTokenResponse refreshToken(String refreshToken) throws IOException { String clientId = getConfiguration().getClientId(); String clientSecret = getConfiguration().getClientSecret(); if (clientId == null || clientSecret == null) { return null; }//from w w w. ja v a 2s . c o m if (refreshToken == null) { refreshToken = getConfiguration().getRefreshToken(); } GoogleRefreshTokenRequest refresh = new GoogleRefreshTokenRequest(httpTransport, jsonFactory, refreshToken, clientId, clientSecret); return refresh.execute(); }
From source file:com.github.lbroudoux.elasticsearch.river.drive.connector.DriveConnector.java
License:Apache License
/** * Actually connect to specified drive, exchanging refresh token for an up-to-date * set of credentials. If folder name specified, we also retrieve subfolders to scan. * @param folderName The name of the root folder to scan. *///from w ww .j a v a2s . c o m public void connectUserDrive(String folderName) throws IOException { this.folderName = folderName; logger.info("Establishing connection to Google Drive"); // We'll use some transport and json factory for sure. HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); TokenResponse tokenResponse = null; try { tokenResponse = new GoogleRefreshTokenRequest(httpTransport, jsonFactory, refreshToken, clientId, clientSecret).execute(); } catch (IOException ioe) { logger.error("IOException while refreshing a token request", ioe); } GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport) .setJsonFactory(jsonFactory).setClientSecrets(clientId, clientSecret).build() .setFromTokenResponse(tokenResponse); //credential.setRefreshToken(refreshToken); service = new Drive.Builder(httpTransport, jsonFactory, credential).build(); logger.info("Connection established."); if (folderName != null) { logger.info("Retrieving scanned subfolders under folder {}, this may take a while...", folderName); subfoldersId = getSubfoldersId(folderName); logger.info("Subfolders to scan found"); if (logger.isDebugEnabled()) { logger.debug("Found {} valid subfolders under folder {}", subfoldersId.size(), folderName); } } }
From source file:com.google.sites.liberation.util.Auth.java
License:Apache License
/** * Method refreshes access token by given REFRESH_TOKEN *//* ww w .java2 s . c o m*/ public 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(); LOGGER.debug("New accessToken (" + response.getAccessToken() + ") has been downloaded (by refreshToken in token file) with Google API Authorization!"); } catch (TokenResponseException e) { LOGGER.warn("Downloaded accessToken is empty! Please check provided token (propriety of -t parameter)"); 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()); }
From source file:com.otway.picasasync.webclient.GoogleOAuth.java
License:Apache License
public Credential getRefreshedCredentials(String refreshCode) throws IOException, GeneralSecurityException { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); log.info("Getting access token for refresh token.."); try {// w w w . j av a 2s . c o m GoogleTokenResponse response = new GoogleRefreshTokenRequest(httpTransport, jsonFactory, refreshCode, clientId, clientSecret).execute(); return new GoogleCredential().setAccessToken(response.getAccessToken()); } catch (UnknownHostException ex) { log.error("Unknown host. No web access?"); throw ex; } catch (IOException e) { log.error("Exception getting refreshed auth: ", e); } return null; }
From source file:model.GoogleUtils.java
/** * Authenticates the service with SSTDodgeball credentials. * /*w w w.j a va 2 s . c o m*/ * @throws IOException */ public static void authenticate() throws IOException { NetHttpTransport transport = new NetHttpTransport(); JacksonFactory jsonFactory = new JacksonFactory(); String accessToken = new GoogleRefreshTokenRequest(transport, jsonFactory, REFRESH_TOKEN, CLIENT_ID, CLIENT_SECRET).execute().getAccessToken(); Credential cred = new GoogleCredential.Builder().setClientSecrets(CLIENT_ID, CLIENT_SECRET) .setJsonFactory(jsonFactory).setTransport(transport).build().setAccessToken(accessToken) .setRefreshToken(REFRESH_TOKEN); service.setOAuth2Credentials(cred); }
From source file:org.gatein.security.oauth.google.GoogleProcessorImpl.java
License:Open Source License
@Override public void refreshToken(GoogleAccessTokenContext accessTokenContext) { GoogleTokenResponse tokenData = accessTokenContext.getTokenData(); if (tokenData.getRefreshToken() == null) { throw new OAuthException(OAuthExceptionCode.GOOGLE_ERROR, "Given GoogleTokenResponse does not contain refreshToken"); }/*from ww w.j av a2 s. c o m*/ 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 OAuthException(OAuthExceptionCode.GOOGLE_ERROR, ioe); } }
From source file:org.picketlink.social.standalone.google.GoogleProcessor.java
License:Apache License
/** * Refresh existing access token. Parameter must have attached refreshToken. New refreshed accessToken will be updated to this * instance of accessTokenContext/*from w ww . j ava2 s. c o m*/ * * @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); } }
From source file:org.sociotech.communitymashup.source.google.urlshortener.GoogleUrlShortenerSourceService.java
License:Open Source License
/** * Refreshes the access token if it expires soon *//*from ww w . j a v a 2 s. c o m*/ private void refreshTokenIfNeeded() { // is expired or expires in the next 5 minutes if (expirationTime - System.currentTimeMillis() < 5000) { // refresh token log("Access token is expired, so refresh it", LogService.LOG_DEBUG); GoogleRefreshTokenRequest tokenRequest = new GoogleRefreshTokenRequest(new NetHttpTransport(), new JacksonFactory(), refreshToken, clientID, clientSecret); // execute request try { // indicate token update tokenUpdate = true; GoogleTokenResponse googleToken = tokenRequest.execute(); expirationTime = System.currentTimeMillis() + 1000 * googleToken.getExpiresInSeconds(); // store access token accessToken = googleToken.getAccessToken(); source.addProperty(GoogleUrlShortenerProperties.ACCESS_TOKEN_PROPERTY, accessToken); // store access token expiration date source.addProperty(GoogleUrlShortenerProperties.ACCESS_TOKEN_EXPIRATION_PROPERTY, "" + expirationTime); // recreate shortener instance instantiateAuthorizedShortener(); } catch (IOException e) { log("Could not refresh access token (" + e.getMessage() + ")", LogService.LOG_WARNING); } finally { // finished token update tokenUpdate = false; } } }