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

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

Introduction

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

Prototype

@Beta
public static GoogleCredential getApplicationDefault(HttpTransport transport, JsonFactory jsonFactory)
        throws IOException 

Source Link

Document

Beta
Returns the Application Default Credentials.

Usage

From source file:ListLogs.java

License:Apache License

/**
 * Returns an authorized Cloud Logging API service client that is usable
 * on Google App Engine, Google Compute Engine, workstations with the Google Cloud SDK,
 * and other computers if you install service account private credentials.
 * See https://cloud.google.com/logging/docs/api/tasks.
 *///from   ww  w.j a va 2  s  . com
// [START auth]
public static Logging getLoggingService() throws IOException {
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(LOGGING_SCOPES);
    }
    Logging service = new Logging.Builder(transport, jsonFactory, credential)
            .setApplicationName(APPLICATION_NAME).build();
    return service;
}

From source file: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);

    // 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);
    }//www  .jav  a2  s . c  o m

    return new Storage.Builder(transport, jsonFactory, credential).setApplicationName("GCS Samples").build();
}

From source file:bqutils.BigQueryServiceFactory.java

License:Apache License

/**
 * Creates an authorized client to Google BigQuery.
 *
 * @return The BigQuery Service// w  w  w. j  a v  a  2s . c  o  m
 * @throws IOException Thrown if there is an error connecting
 */
// [START get_service]
private static Bigquery createAuthorizedClient() throws IOException {
    // Create the credential
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

    // 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 BigQuery scope if required.
    if (credential.createScopedRequired()) {
        Collection<String> bigqueryScopes = BigqueryScopes.all();
        credential = credential.createScoped(bigqueryScopes);
    }

    return new Bigquery.Builder(transport, jsonFactory, credential).setApplicationName("BigQuery Samples")
            .build();
}

From source file:com.appsflyer.spark.bigquery.BigQueryServiceFactory.java

License:Apache License

/**
 * Creates an authorized client to Google BigQuery.
 *
 * @return The BigQuery Service/*from   w  w  w. j av a2s .c o m*/
 * @throws IOException Thrown if there is an error connecting
 */
private static Bigquery createAuthorizedClient() throws IOException {
    // Create the credential
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

    // 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 BigQuery scope if required.
    if (credential.createScopedRequired()) {
        Collection<String> bigqueryScopes = BigqueryScopes.all();
        credential = credential.createScoped(bigqueryScopes);
    }

    return new Bigquery.Builder(transport, jsonFactory, credential).setApplicationName("BigQuery Spark Stream")
            .build();
}

From source file:com.bucketoperations.controller.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);

    // 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);
    }/*from   www  .j  a v  a  2s .  co  m*/

    return new Storage.Builder(transport, jsonFactory, credential).setApplicationName("Data_upload").build();
}

From source file:com.cloudera.director.google.internal.GoogleCredentials.java

License:Apache License

private Compute buildCompute() {
    try {/*  ww w. j  a v  a  2  s  .co  m*/
        JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        GoogleCredential credential;

        if (jsonKey != null) {
            credential = GoogleCredential
                    .fromStream(new ByteArrayInputStream(jsonKey.getBytes()), httpTransport, JSON_FACTORY)
                    .createScoped(Collections.singleton(ComputeScopes.COMPUTE));
        } else {
            Collection COMPUTE_SCOPES = Collections.singletonList(ComputeScopes.COMPUTE);

            credential = GoogleCredential.getApplicationDefault(httpTransport, JSON_FACTORY)
                    .createScoped(COMPUTE_SCOPES);
        }

        return new Compute.Builder(httpTransport, JSON_FACTORY, null)
                .setApplicationName(Names.buildApplicationNameVersionTag(applicationProperties))
                .setHttpRequestInitializer(credential).build();

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

From source file:com.cloudera.director.google.internal.GoogleCredentials.java

License:Apache License

private SQLAdmin buildSQLAdmin() {
    try {//from  w w w.  jav a  2  s . co  m
        JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        GoogleCredential credential;

        if (jsonKey != null) {
            credential = GoogleCredential
                    .fromStream(new ByteArrayInputStream(jsonKey.getBytes()), httpTransport, JSON_FACTORY)
                    .createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN));
        } else {
            Collection SQLSERVICE_ADMIN_SCOPES = Collections.singletonList(SQLAdminScopes.SQLSERVICE_ADMIN);

            credential = GoogleCredential.getApplicationDefault(httpTransport, JSON_FACTORY)
                    .createScoped(SQLSERVICE_ADMIN_SCOPES);
        }

        return new SQLAdmin.Builder(httpTransport, JSON_FACTORY, null)
                .setApplicationName(Names.buildApplicationNameVersionTag(applicationProperties))
                .setHttpRequestInitializer(credential).build();

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

From source file:com.dataartisans.flink.dataflow.util.PortableConfiguration.java

License:Apache License

public static Pubsub createPubsubClient(HttpTransport httpTransport, JsonFactory jsonFactory)
        throws IOException {
    Preconditions.checkNotNull(httpTransport);
    Preconditions.checkNotNull(jsonFactory);
    GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
    // In some cases, you need to add the scope explicitly.
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(PubsubScopes.all());
    }/*from   ww w.  j a v a  2  s  .  c o  m*/
    // 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 Pubsub.Builder(httpTransport, jsonFactory, initializer).build();
}

From source file:com.example.CryptFile.java

License:Apache License

/**
 * Creates an authorized CloudKMS client service using Application Default Credentials.
 *
 * @return an authorized CloudKMS client
 * @throws IOException if there's an error getting the default credentials.
 *///from  w  w  w .  j a  v  a  2  s  . co  m
public static CloudKMS createAuthorizedClient() throws IOException {
    // Create the credential
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    // Authorize the client using Application Default Credentials
    // @see https://g.co/dv/identity/protocols/application-default-credentials
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

    // 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 scope if required.
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(CloudKMSScopes.all());
    }

    return new CloudKMS.Builder(transport, jsonFactory, credential).setApplicationName("CloudKMS CryptFile")
            .build();
}

From source file:com.example.Quickstart.java

License:Apache License

/**
 * Creates an authorized CloudKMS client service using Application Default Credentials.
 *
 * @return an authorized CloudKMS client
 * @throws IOException if there's an error getting the default credentials.
 *///from w w  w .j  av  a 2 s  .  c  o m
public static CloudKMS createAuthorizedClient() throws IOException {
    // Create the credential
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    // Authorize the client using Application Default Credentials
    // @see https://g.co/dv/identity/protocols/application-default-credentials
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

    // 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 scope if required.
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(CloudKMSScopes.all());
    }

    return new CloudKMS.Builder(transport, jsonFactory, credential).setApplicationName("CloudKMS snippets")
            .build();
}