String accessToken = credential.getAccessToken();
String accessToken = credential.getAccessToken();
/** * Do an HTTPS HEAD by appending the application default token to the URL. * * @param url * @return response body as string * @throws IOException */ public static String doHead(String url) throws IOException { LOG.debug("Url for HEAD: " + url); String authUrl = url + "?access_token=" + GoogleCredential.getApplicationDefault().getAccessToken(); HttpURLConnection con = (HttpURLConnection) new URL(authUrl).openConnection(); con.setDoInput(true); con.setRequestMethod("HEAD"); int code = con.getResponseCode(); LOG.debug("Response code: " + code); if (code != HttpURLConnection.HTTP_OK) { throw new IOException("HTTP error: " + code + " for url "); } return FileUtils.readAll(con.getInputStream()); } }
GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute(); GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport) .setJsonFactory(jsonFactory) .setClientSecrets(CLIENT_ID, CLIENT_SECRET) .build() .setFromTokenResponse(response); String accessToken = credential.getAccessToken(); String refreshToken = credential.getRefreshToken();
/** * Do an HTTPS GET by appending the application default token to the URL. * * @param url * @return response body as string * @throws IOException */ public static String doGet(String url) throws IOException { LOG.debug("Url for GET: " + url); String authUrl = url + "?access_token=" + GoogleCredential.getApplicationDefault().getAccessToken(); HttpURLConnection con = (HttpURLConnection) new URL(authUrl).openConnection(); con.setDoInput(true); con.setRequestMethod("GET"); int code = con.getResponseCode(); LOG.debug("Response code: " + code); if (code != HttpURLConnection.HTTP_OK) { String msg = FileUtils.readAll(con.getErrorStream()); throw new IOException("HTTP error: " + code + " for url " + url + "" + msg); } return FileUtils.readAll(con.getInputStream()); }
/** * Do an HTTPS DELETE by appending the application default token to the URL. * * @param url * @return response body as string * @throws IOException */ public static String doDelete(String gcsPath) throws IOException { if (gcsPath == null || !gcsPath.startsWith("gs://")) { throw new IOException("GCS path must be non-null and start with gs://. Value: " + gcsPath); } String url = "https://storage.googleapis.com/" + gcsPath.substring("gs://".length()); LOG.debug("Url for DELETE: " + url); String authUrl = url + "?access_token=" + GoogleCredential.getApplicationDefault().getAccessToken(); HttpURLConnection con = (HttpURLConnection) new URL(authUrl).openConnection(); con.setDoInput(true); con.setRequestMethod("DELETE"); int code = con.getResponseCode(); LOG.debug("Response code: " + code); if (code != HttpURLConnection.HTTP_OK && code != HttpURLConnection.HTTP_NO_CONTENT) { String msg = FileUtils.readAll(con.getErrorStream()); throw new IOException("HTTP error: " + code + " for url " + url + "" + msg); } return FileUtils.readAll(con.getInputStream()); }
LOG.debug("Url for POST: " + url); String authUrl = url + "?access_token=" + GoogleCredential.getApplicationDefault().getAccessToken();
String token = credential.getAccessToken();
final GoogleCredential credential = new Builder() .setTransport(new NetHttpTransport()) .setJsonFactory(new JacksonFactory()) .setClientSecrets(OAuth2Provider.GOOGLE_CLIENT_ID, OAuth2Provider.GOOGLE_CLIENT_SECRET) .build().setRefreshToken(refreshToken); credential.refreshToken(); //do not forget to call String newAccessToken = credential.getAccessToken();
String accessToken = credential.getAccessToken();
@Override public Optional<GoogleAccessToken> getGoogleAccessToken() { if ( !getGoogleCredential().isPresent() ) { return Optional.empty(); } GoogleCredential credential = getGoogleCredential().get(); try { if ( !credential.refreshToken() || credential.getExpiresInSeconds() == null ) { log.warn( "There is no refresh token to be retrieved" ); return Optional.empty(); } } catch ( IOException ex ) { throw new IllegalStateException( "Could not retrieve refresh token: " + ex.getMessage(), ex ); } GoogleAccessToken token = new GoogleAccessToken(); token.setAccessToken( credential.getAccessToken() ); token.setClientId( getProperty( ConfigurationKey.GOOGLE_SERVICE_ACCOUNT_CLIENT_ID ) ); token.setExpiresInSeconds( credential.getExpiresInSeconds() ); token.setExpiresOn( LocalDateTime.now().plusSeconds( token.getExpiresInSeconds() ) ); return Optional.of( token ); }
); System.out.println("OAuth token: " + GoogleCredential.getApplicationDefault().getAccessToken() + "\n"); return;
@Override protected Tokeninfo invokeRequest(GoogleAccessTokenContext accessTokenContext) throws IOException { GoogleTokenResponse tokenData = accessTokenContext.getTokenData(); Oauth2 oauth2 = getOAuth2Instance(accessTokenContext); GoogleCredential credential = getGoogleCredential(tokenData); return oauth2.tokeninfo().setAccessToken(credential.getAccessToken()).execute(); }
.set("access_token", credential.getAccessToken()) .set("alt", "media").build();