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

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

Introduction

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

Prototype

@Beta
public boolean createScopedRequired() 

Source Link

Document

Beta
Indicates whether the credential requires scopes to be specified by calling createScoped before use.

Usage

From source file:io.konig.gcp.common.GoogleCloudService.java

License:Apache License

public SQLAdmin sqlAdmin() {
    if (sqlAdmin == null) {
        try {/* ww w . jav a 2s  .c o m*/

            HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

            GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(credentialsFile));
            if (credential.createScopedRequired()) {
                credential = credential
                        .createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
            }
            SQLAdmin.Builder builder = new SQLAdmin.Builder(httpTransport, jsonFactory, credential);
            builder = builder.setApplicationName(projectId);
            sqlAdmin = builder.build();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    return sqlAdmin;
}

From source file:io.rodeo.chute.bigquery.BigQueryExporter.java

License:Apache License

public BigQueryExporter(BigQueryExporterConfiguration config) {
    this.config = config;
    this.checkedSchemas = new HashSet<String>();
    this.existingSchemaMap = new HashMap<String, com.google.api.services.bigquery.model.TableSchema>();

    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential;
    try {//from  w  ww.j a va  2s . co m
        credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(BigqueryScopes.all());
    }
    this.bq = new Bigquery.Builder(transport, jsonFactory, credential)
            .setApplicationName(this.config.applicationName).build();
}

From source file:mx.gob.hidalgo.repss.planeacion.services.google.GoogleCloudStorage.java

License:Apache License

/**
 * Returns an authenticated Storage object used to make service calls to Cloud Storage.
 *//*from  w  w  w.ja v a  2  s .c o m*/
private static Storage getService() throws IOException, GeneralSecurityException {
    if (null == storageService) {
        GoogleCredential credential = GoogleCredential.getApplicationDefault();
        // Depending on the environment that provides the default credentials (e.g. 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()) {
            credential = credential.createScoped(StorageScopes.all());
        }
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        storageService = new Storage.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();
    }
    return storageService;
}

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

License:Apache License

@Provides
@LazySingleton/*  w w  w .  j  a  v a 2 s.c om*/
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  w  w  w  . j ava2 s.  c  o  m

    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 a2  s. com*/
 * @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.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//from  ww w. jav  a 2s  .  co 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();
}