List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleCredential createScopedRequired
@Beta public boolean createScopedRequired()
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()); }/* w w w. ja v a 2 s. c o m*/ 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 {//from www . j a va 2 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 {/*w w w. j ava 2s.co 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")); } 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 {//from w w w . ja v a 2s . 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 {//w w w. j a v a 2 s . 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 www . ja v a2 s.c o 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); }//w w w. ja va 2s .c om return credential; }
From source file:domain.bsu.dektiarev.service.StorageFactory.java
License:Open Source License
private static Storage buildService() throws IOException, GeneralSecurityException { HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = new JacksonFactory(); /*GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);*/ GoogleCredential credential = GoogleCredential .fromStream(new FileInputStream("resources/APNewsFeed-7e0946ab6fb1.json")) .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_READ_WRITE)); if (credential.createScopedRequired()) { Collection<String> bigqueryScopes = StorageScopes.all(); credential = credential.createScoped(bigqueryScopes); }//from w w w. j av a 2 s . co m return new Storage.Builder(transport, jsonFactory, credential).setApplicationName("newsfeedforhybridcloud") .build(); }
From source file:fi.gapps.intra.thesis.controller.TaskQueueSample.java
License:Open Source License
public static void run() throws Exception { parseParams();//www .j av a2 s. c om httpTransport = GoogleNetHttpTransport.newTrustedTransport(); // [START auth_and_intitalize_client] // Authenticate using Google Application Default Credentials. GoogleCredential credential = GoogleCredential.getApplicationDefault(); if (credential.createScopedRequired()) { List<String> scopes = new ArrayList<>(); // Set TaskQueue Scopes scopes.add(TaskqueueScopes.TASKQUEUE); scopes.add(TaskqueueScopes.TASKQUEUE_CONSUMER); credential = credential.createScoped(scopes); } // Intialize Taskqueue object. Taskqueue taskQueue = new Taskqueue.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build(); // [END auth_and_intitalize_client] /* * Get the task queue using the name of an existing task queue listed in * the App Engine Task Queue section of the Developers console. See the * following sample for an example App Engine app that creates a pull * task queue: * https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/ * appengine/taskqueue */ com.google.api.services.taskqueue.model.TaskQueue queue = getQueue(taskQueue); System.out.println("================== Listing Task Queue details =================="); System.out.println(queue); // Lease, process and delete tasks // [START lease_tasks] Tasks tasks = getLeasedTasks(taskQueue); if (tasks.getItems() == null || tasks.getItems().size() == 0) { System.out.println("No tasks to lease, so now exiting"); } else { for (Task leasedTask : tasks.getItems()) { processTask(leasedTask); deleteTask(taskQueue, leasedTask); } } // [END lease_tasks] }
From source file:io.druid.storage.google.GoogleStorageDruidModule.java
License:Apache License
@Provides @LazySingleton/*from w w w . j a v a 2s . c o m*/ public GoogleStorage getGoogleStorage(final GoogleAccountConfig config) throws IOException, GeneralSecurityException { LOG.info("Building Cloud Storage Client..."); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); if (credential.createScopedRequired()) { credential = credential.createScoped(StorageScopes.all()); } Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential) .setApplicationName(APPLICATION_NAME).build(); return new GoogleStorage(storage); }