List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleCredential createScoped
@Beta
public GoogleCredential createScoped(Collection<String> scopes)
From source file:com.google.cloud.sparkdemo.BigqueryWriter.java
License:Apache License
private Bigquery createAuthorizedClient() { try {/*from www. j a v a 2 s .co m*/ // Create the credential HttpTransport transport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); if (credential.createScopedRequired()) { Collection<String> bigqueryScopes = BigqueryScopes.all(); credential = credential.createScoped(bigqueryScopes); } return new Bigquery.Builder(transport, jsonFactory, credential) .setApplicationName("Spark Gaming Samples").build(); } catch (IOException e) { // can not create bigquery client e.printStackTrace(); return null; } }
From source file:com.google.cloud.sparkdemo.CloudPubsubReceiver.java
License:Apache License
private Pubsub createAuthorizedClient() { try {/*from w ww.j a v a 2 s . c o m*/ // Create the credential HttpTransport httpTransport = Utils.getDefaultTransport(); JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); if (credential.createScopedRequired()) { credential = credential.createScoped(PubsubScopes.all()); } HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Pubsub.Builder(httpTransport, jsonFactory, initializer) .setApplicationName("spark-pubsub-receiver").build(); } catch (IOException e) { reportError("Unable to create Cloud Pub/sub client.", e); return null; } }
From source file:com.google.cloud.storage.storagetransfer.samples.TransferClientCreator.java
License:Open Source License
/** * Create a Storage Transfer client using user-supplied credentials and other settings. * * @param httpTransport/*from w w w .j a v a 2 s. c o m*/ * a user-supplied HttpTransport * @param jsonFactory * a user-supplied JsonFactory * @param credential * a user-supplied Google credential * @return a Storage Transfer client */ public static Storagetransfer createStorageTransferClient(HttpTransport httpTransport, JsonFactory jsonFactory, GoogleCredential credential) { Preconditions.checkNotNull(httpTransport); Preconditions.checkNotNull(jsonFactory); Preconditions.checkNotNull(credential); // In some cases, you need to add the scope explicitly. if (credential.createScopedRequired()) { credential = credential.createScoped(StoragetransferScopes.all()); } // Please use custom HttpRequestInitializer for automatic // retry upon failures. We provide a simple reference // implementation in the "Retry Handling" section. HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Storagetransfer.Builder(httpTransport, jsonFactory, initializer) .setApplicationName("storagetransfer-sample").build(); }
From source file:com.google.crypto.tink.integration.gcpkms.GcpKmsClient.java
License:Apache License
/** Loads the provided credential. */ public KmsClient withCredentials(GoogleCredential credential) { if (credential.createScopedRequired()) { credential = credential.createScoped(CloudKMSScopes.all()); }/*from w w w . j av a 2 s .com*/ this.client = new CloudKMS.Builder(new NetHttpTransport(), new JacksonFactory(), credential) .setApplicationName(APPLICATION_NAME).build(); return this; }
From source file:com.google.pubsub.clients.common.MetricsHandler.java
License:Apache License
private void initialize() { synchronized (this) { try {/* w ww .j av a2 s.c o m*/ HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); if (credential.createScopedRequired()) { credential = credential.createScoped( Collections.singletonList("https://www.googleapis.com/auth/cloud-platform")); } monitoring = new Monitoring.Builder(transport, jsonFactory, credential) .setApplicationName("Cloud Pub/Sub Loadtest Framework").build(); String zoneId; String instanceId; try { DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.addRequestInterceptor(new RequestAcceptEncoding()); httpClient.addResponseInterceptor(new ResponseContentEncoding()); HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 30000); HttpConnectionParams.setSoTimeout(httpClient.getParams(), 30000); HttpConnectionParams.setSoKeepalive(httpClient.getParams(), true); HttpConnectionParams.setStaleCheckingEnabled(httpClient.getParams(), false); HttpConnectionParams.setTcpNoDelay(httpClient.getParams(), true); SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); httpClient.setKeepAliveStrategy((response, ctx) -> 30); HttpGet zoneIdRequest = new HttpGet( "http://metadata.google.internal/computeMetadata/v1/instance/zone"); zoneIdRequest.setHeader("Metadata-Flavor", "Google"); HttpResponse zoneIdResponse = httpClient.execute(zoneIdRequest); String tempZoneId = EntityUtils.toString(zoneIdResponse.getEntity()); if (tempZoneId.lastIndexOf("/") >= 0) { zoneId = tempZoneId.substring(tempZoneId.lastIndexOf("/") + 1); } else { zoneId = tempZoneId; } HttpGet instanceIdRequest = new HttpGet( "http://metadata.google.internal/computeMetadata/v1/instance/id"); instanceIdRequest.setHeader("Metadata-Flavor", "Google"); HttpResponse instanceIdResponse = httpClient.execute(instanceIdRequest); instanceId = EntityUtils.toString(instanceIdResponse.getEntity()); } catch (IOException e) { log.info("Unable to connect to metadata server, assuming not on GCE, setting " + "defaults for instance and zone."); instanceId = "local"; zoneId = "us-east1-b"; // Must use a valid cloud zone even if running local. } monitoredResource.setLabels( ImmutableMap.of("project_id", project, "instance_id", instanceId, "zone", zoneId)); createMetrics(); } catch (IOException e) { log.error("Unable to initialize MetricsHandler, trying again.", e); executor.execute(this::initialize); } catch (GeneralSecurityException e) { log.error("Unable to initialize MetricsHandler permanently, credentials error.", e); } } }
From source file:com.google.pubsub.flic.controllers.GCEController.java
License:Apache License
/** Returns a GCEController using default application credentials. */ public static GCEController newGCEController(String projectName, Map<String, Map<ClientParams, Integer>> types, ScheduledExecutorService executor) { try {/*from w ww .j a va 2 s . c om*/ HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); if (credential.createScopedRequired()) { credential = credential .createScoped(Collections.singletonList("https://www.googleapis.com/auth/cloud-platform")); } return new GCEController(projectName, types, executor, new Storage.Builder(transport, jsonFactory, credential) .setApplicationName("Cloud Pub/Sub Loadtest Framework").build(), new Compute.Builder(transport, jsonFactory, credential) .setApplicationName("Cloud Pub/Sub Loadtest Framework").build(), new Pubsub.Builder(transport, jsonFactory, credential) .setApplicationName("Cloud Pub/Sub Loadtest Framework").build()); } catch (Throwable t) { return null; } }
From source file:com.netflix.spinnaker.halyard.backup.kms.v1.google.GoogleKms.java
License:Apache License
private static GoogleCredential loadKmsCredential(HttpTransport transport, JsonFactory factory, String jsonPath) throws IOException { GoogleCredential credential; if (!jsonPath.isEmpty()) { FileInputStream stream = new FileInputStream(jsonPath); credential = GoogleCredential.fromStream(stream, transport, factory); log.info("Loaded kms credentials from " + jsonPath); } else {// w w w . j av a2 s.co m log.info("Using kms default application credentials."); credential = GoogleCredential.getApplicationDefault(); } if (credential.createScopedRequired()) { credential = credential.createScoped(CloudKMSScopes.all()); } return credential; }
From source file:com.netflix.spinnaker.halyard.backup.kms.v1.google.GoogleStorage.java
License:Apache License
private static GoogleCredential loadStorageCredential(HttpTransport transport, JsonFactory factory, String jsonPath) throws IOException { GoogleCredential credential; if (!jsonPath.isEmpty()) { FileInputStream stream = new FileInputStream(jsonPath); credential = GoogleCredential.fromStream(stream, transport, factory); log.info("Loaded storage credentials from " + jsonPath); } else {//from w w w .ja v a 2s . c o m log.info("Using storage default application credentials."); credential = GoogleCredential.getApplicationDefault(); } if (credential.createScopedRequired()) { credential = credential.createScoped(StorageScopes.all()); } return credential; }
From source file:com.netflix.spinnaker.halyard.core.registry.v1.GoogleProfileReader.java
License:Apache License
private Storage createGoogleStorage(boolean useApplicationDefaultCreds) { JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); String applicationName = "Spinnaker/Halyard"; HttpRequestInitializer requestInitializer; try {/*from w ww.j ava 2 s. co m*/ GoogleCredential credential = useApplicationDefaultCreds ? GoogleCredential.getApplicationDefault() : new GoogleCredential(); if (credential.createScopedRequired()) { credential = credential.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)); } requestInitializer = GoogleCredentials.setHttpTimeout(credential); log.info("Loaded application default credential for reading BOMs & profiles."); } catch (Exception e) { requestInitializer = GoogleCredentials.retryRequestInitializer(); log.debug( "No application default credential could be loaded for reading BOMs & profiles. Continuing unauthenticated: {}", e.getMessage()); } return new Storage.Builder(GoogleCredentials.buildHttpTransport(), jsonFactory, requestInitializer) .setApplicationName(applicationName).build(); }
From source file:com.spotify.google.cloud.pubsub.client.Pubsub.java
License:Apache License
private Credential scoped(final GoogleCredential credential) { if (credential.createScopedRequired()) { return credential.createScoped(SCOPES); }//from www . j av a2 s .c o m return credential; }