Example usage for com.google.api.client.googleapis.auth.oauth2 GoogleCredential createScoped

List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleCredential createScoped

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.auth.oauth2 GoogleCredential createScoped.

Prototype

@Beta
public GoogleCredential createScoped(Collection<String> scopes) 

Source Link

Document

Beta
For credentials that require scopes, creates a copy of the credential with the specified scopes.

Usage

From source file:org.apache.druid.common.gcp.GcpModule.java

License:Apache License

@Provides
@LazySingleton/*  www .  j a  v  a2 s.co  m*/
public HttpRequestInitializer getHttpRequestInitializer(HttpTransport transport, JsonFactory factory)
        throws IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, factory);
    if (credential.createScopedRequired()) {
        credential = credential
                .createScoped(Collections.singleton("https://www.googleapis.com/auth/cloud-platform"));
    }
    return credential;
}

From source file:org.apache.zeppelin.bigquery.BigQueryInterpreter.java

License:Apache License

private static Bigquery createAuthorizedClient() throws IOException {
    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);
    }/*from  ww w.j av a 2s. c om*/

    return new Bigquery.Builder(transport, jsonFactory, credential)
            .setApplicationName("Zeppelin/1.0 (GPN:Apache Zeppelin;)").build();
}

From source file:org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.java

License:Apache License

/**
 * Loads the service account file corresponding to a given client name. If no file is defined for the client,
 * a {@code null} credential is returned.
 *
 * @param settings the {@link Settings}/*from  www .  j  a v  a2s.c  om*/
 * @param clientName the client name
 *
 * @return the {@link GoogleCredential} to use for the given client, {@code null} if no service account is defined.
 */
static GoogleCredential loadCredential(final Settings settings, final String clientName) {
    try {
        if (CREDENTIALS_FILE_SETTING.getConcreteSettingForNamespace(clientName).exists(settings) == false) {
            // explicitly returning null here so that the default credential
            // can be loaded later when creating the Storage client
            return null;
        }
        try (InputStream credStream = CREDENTIALS_FILE_SETTING.getConcreteSettingForNamespace(clientName)
                .get(settings)) {
            GoogleCredential credential = GoogleCredential.fromStream(credStream);
            if (credential.createScopedRequired()) {
                credential = credential
                        .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL));
            }
            return credential;
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.gradle.internal.resource.transport.gcp.gcs.GcsClient.java

License:Apache License

private static Supplier<Credential> getCredentialSupplier(final HttpTransport transport,
        final JsonFactory jsonFactory) {
    return Suppliers.memoize(new Supplier<Credential>() {
        @Override/*w ww . j a  v  a 2s.c  o m*/
        public Credential get() {
            try {
                GoogleCredential googleCredential = GoogleCredential.getApplicationDefault(transport,
                        jsonFactory);
                // Ensure we have a scope
                return googleCredential
                        .createScoped(singletonList("https://www.googleapis.com/auth/devstorage.read_write"));
            } catch (IOException e) {
                throw new UncheckedIOException("Failed to get Google credentials for GCS connection", e);
            }
        }
    });
}

From source file:org.jenkinsci.plugins.googlecloudlogging.manager.BigQueryManager.java

License:Apache License

/**
 * Creates an authorized BigQuery builder using the Application Default Credentials.
 *
 * @return an authorized BigQuery builder
 *
 * @throws IOException//ww w .  j  a v a  2  s  .c o  m
 */
private static Bigquery createAuthorizedClient() throws IOException {
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

    if (credential.createScopedRequired()) {
        credential = credential.createScoped(BigqueryScopes.all());
    }

    return new Bigquery.Builder(transport, jsonFactory, credential)
            .setApplicationName(GoogleCloudLoggingConstants.APPLICATION_NAME).build();
}