Java Code Examples for com.google.auth.oauth2.GoogleCredentials#createScopedRequired()
The following examples show how to use
com.google.auth.oauth2.GoogleCredentials#createScopedRequired() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleStorage.java From halyard with Apache License 2.0 | 7 votes |
private static GoogleCredentials loadStorageCredential(String jsonPath) throws IOException { GoogleCredentials credentials; if (!jsonPath.isEmpty()) { FileInputStream stream = new FileInputStream(jsonPath); credentials = GoogleCredentials.fromStream(stream); log.info("Loaded storage credentials from " + jsonPath); } else { log.info("Using storage default application credentials."); credentials = GoogleCredentials.getApplicationDefault(); } if (credentials.createScopedRequired()) { credentials = credentials.createScoped(StorageScopes.all()); } return credentials; }
Example 2
Source File: GoogleKms.java From halyard with Apache License 2.0 | 6 votes |
private static GoogleCredentials loadKmsCredential(String jsonPath) throws IOException { GoogleCredentials credentials; if (!jsonPath.isEmpty()) { FileInputStream stream = new FileInputStream(jsonPath); credentials = GoogleCredentials.fromStream(stream); log.info("Loaded kms credentials from " + jsonPath); } else { log.info("Using kms default application credentials."); credentials = GoogleCredentials.getApplicationDefault(); } if (credentials.createScopedRequired()) { credentials = credentials.createScoped(CloudKMSScopes.all()); } return credentials; }
Example 3
Source File: CredentialModule.java From nomulus with Apache License 2.0 | 6 votes |
/** * Provides the default {@link GoogleCredentialsBundle} from the Google Cloud runtime. * * <p>The credential returned depends on the runtime environment: * * <ul> * <li>On AppEngine, returns the service account credential for * [email protected] * <li>On Compute Engine, returns the service account credential for * [email protected] * <li>On end user host, this returns the credential downloaded by gcloud. Please refer to <a * href="https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login">Cloud * SDK documentation</a> for details. * </ul> */ @DefaultCredential @Provides @Singleton public static GoogleCredentialsBundle provideDefaultCredential( @Config("defaultCredentialOauthScopes") ImmutableList<String> requiredScopes) { GoogleCredentials credential; try { credential = GoogleCredentials.getApplicationDefault(); } catch (IOException e) { throw new RuntimeException(e); } if (credential.createScopedRequired()) { credential = credential.createScoped(requiredScopes); } return GoogleCredentialsBundle.create(credential); }
Example 4
Source File: CredentialModule.java From nomulus with Apache License 2.0 | 6 votes |
/** * Provides a {@link GoogleCredentialsBundle} from the service account's JSON key file. * * <p>On App Engine, a thread created using Java's built-in API needs this credential when it * calls App Engine API. The Google Sheets API also needs this credential. */ @JsonCredential @Provides @Singleton public static GoogleCredentialsBundle provideJsonCredential( @Config("defaultCredentialOauthScopes") ImmutableList<String> requiredScopes, @Key("jsonCredential") String jsonCredential) { GoogleCredentials credential; try { credential = GoogleCredentials.fromStream(new ByteArrayInputStream(jsonCredential.getBytes(UTF_8))); } catch (IOException e) { throw new UncheckedIOException(e); } if (credential.createScopedRequired()) { credential = credential.createScoped(requiredScopes); } return GoogleCredentialsBundle.create(credential); }
Example 5
Source File: AuthModule.java From nomulus with Apache License 2.0 | 6 votes |
@Provides @LocalCredential public static GoogleCredentialsBundle provideLocalCredential( @LocalCredentialJson String credentialJson, @Config("localCredentialOauthScopes") ImmutableList<String> scopes) { try { GoogleCredentials credential = GoogleCredentials.fromStream(new ByteArrayInputStream(credentialJson.getBytes(UTF_8))); if (credential.createScopedRequired()) { credential = credential.createScoped(scopes); } return GoogleCredentialsBundle.create(credential); } catch (IOException e) { throw new RuntimeException(e); } }
Example 6
Source File: StorageFactory.java From java-docs-samples with Apache License 2.0 | 6 votes |
private static Storage buildService() throws IOException, GeneralSecurityException { HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleCredentials credential = GoogleCredentials.getApplicationDefault(); // Depending on the environment that provides the default credentials (for // example: Compute Engine, App Engine), the credentials may require us to // specify the scopes we need explicitly. Check for this case, and inject // the Cloud Storage scope if required. if (credential.createScopedRequired()) { Collection<String> scopes = StorageScopes.all(); credential = credential.createScoped(scopes); } return new Storage.Builder(transport, jsonFactory, new HttpCredentialsAdapter(credential)) .setApplicationName("GCS Samples") .build(); }
Example 7
Source File: TransferClientCreator.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** * Create a Storage Transfer client using user-supplied credentials and other settings. * * @param httpTransport 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, GoogleCredentials 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 HttpCredentialsAdapter(credential); return new Storagetransfer.Builder(httpTransport, jsonFactory, initializer) .setApplicationName("storagetransfer-sample") .build(); }
Example 8
Source File: GcsStorageService.java From front50 with Apache License 2.0 | 6 votes |
private GoogleCredentials loadCredential(String jsonPath) throws IOException { GoogleCredentials credentials = null; if (!jsonPath.isEmpty()) { FileInputStream stream = new FileInputStream(jsonPath); credentials = GoogleCredentials.fromStream(stream); log.info("Loaded credentials from {}", value("jsonPath", jsonPath)); } else { log.info( "spinnaker.gcs.enabled without spinnaker.gcs.jsonPath. " + "Using default application credentials. Using default credentials."); credentials = GoogleCredentials.getApplicationDefault(); } return credentials.createScopedRequired() ? credentials.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)) : credentials; }
Example 9
Source File: CoreSocketFactory.java From cloud-sql-jdbc-socket-factory with Apache License 2.0 | 5 votes |
@Override public HttpRequestInitializer create() { GoogleCredentials credentials; try { credentials = GoogleCredentials.getApplicationDefault(); } catch (IOException err) { throw new RuntimeException( "Unable to obtain credentials to communicate with the Cloud SQL API", err); } if (credentials.createScopedRequired()) { credentials = credentials.createScoped(Collections.singletonList(SQLAdminScopes.SQLSERVICE_ADMIN)); } return new HttpCredentialsAdapter(credentials); }
Example 10
Source File: BigqueryClient.java From beam with Apache License 2.0 | 5 votes |
private static Credentials getDefaultCredential() { GoogleCredentials credential; try { credential = GoogleCredentials.getApplicationDefault(); } catch (IOException e) { throw new RuntimeException("Failed to get application default credential.", e); } if (credential.createScopedRequired()) { Collection<String> bigqueryScope = Lists.newArrayList(BigqueryScopes.all()); credential = credential.createScoped(bigqueryScope); } return credential; }
Example 11
Source File: ProxyModule.java From nomulus with Apache License 2.0 | 5 votes |
@Singleton @Provides static GoogleCredentialsBundle provideCredential(ProxyConfig config) { try { GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); if (credentials.createScopedRequired()) { credentials = credentials.createScoped(config.gcpScopes); } return GoogleCredentialsBundle.create(credentials); } catch (IOException e) { throw new RuntimeException("Unable to obtain OAuth2 credential.", e); } }
Example 12
Source File: GoogleUtils.java From kork with Apache License 2.0 | 5 votes |
static GoogleCredentials buildGoogleCredentials() throws IOException { GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); if (credentials.createScopedRequired()) { credentials = credentials.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_READ_ONLY)); } return credentials; }
Example 13
Source File: GoogleIdTokenAuth.java From styx with Apache License 2.0 | 4 votes |
private static GoogleCredentials withScopes(GoogleCredentials credentials, Collection<String> scopes) { if (!credentials.createScopedRequired()) { return credentials; } return credentials.createScoped(scopes); }
Example 14
Source File: ComputeEngineSample.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void main(String[] args) { try { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); // Authenticate using Google Application Default Credentials. GoogleCredentials credential = GoogleCredentials.getApplicationDefault(); if (credential.createScopedRequired()) { List<String> scopes = new ArrayList<>(); // Set Google Cloud Storage scope to Full Control. scopes.add(ComputeScopes.DEVSTORAGE_FULL_CONTROL); // Set Google Compute Engine scope to Read-write. scopes.add(ComputeScopes.COMPUTE); credential = credential.createScoped(scopes); } HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credential); // Create Compute Engine object for listing instances. Compute compute = new Compute.Builder(httpTransport, JSON_FACTORY, requestInitializer) .setApplicationName(APPLICATION_NAME) .build(); // List out instances, looking for the one created by this sample app. boolean foundOurInstance = printInstances(compute); Operation op; if (foundOurInstance) { op = deleteInstance(compute, SAMPLE_INSTANCE_NAME); } else { op = startInstance(compute, SAMPLE_INSTANCE_NAME); } // Call Compute Engine API operation and poll for operation completion status System.out.println("Waiting for operation completion..."); Operation.Error error = blockUntilComplete(compute, op, OPERATION_TIMEOUT_MILLIS); if (error == null) { System.out.println("Success!"); } else { System.out.println(error.toPrettyString()); } } catch (IOException e) { System.err.println(e.getMessage()); } catch (Throwable t) { t.printStackTrace(); } System.exit(1); }