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:com.google.cloud.pubsub.client.demos.appengine.util.PubsubUtils.java

License:Apache License

/**
 * Builds a new Pubsub client and returns it.
 *
 * @param httpTransport HttpTransport for Pubsub client.
 * @param jsonFactory JsonFactory for Pubsub client.
 * @return Pubsub client.//from www  . jav  a  2s .  c  o m
 * @throws IOException when we can not get the default credentials.
 */
public static Pubsub getClient(final HttpTransport httpTransport, final JsonFactory jsonFactory)
        throws IOException {
    Preconditions.checkNotNull(httpTransport);
    Preconditions.checkNotNull(jsonFactory);
    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(PubsubScopes.all());
    }
    // Please use custom HttpRequestInitializer for automatic
    // retry upon failures.
    HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
    return new Pubsub.Builder(httpTransport, jsonFactory, initializer).setApplicationName(APPLICATION_NAME)
            .build();
}

From source file:com.google.cloud.pubsub.proxy.gcloud.GcloudPubsub.java

License:Open Source License

/**
 * Constructor that will automatically instantiate a Google Cloud Pub/Sub instance.
 *
 * @throws IOException when the initialization of the Google Cloud Pub/Sub client fails.
 *///from  ww  w  .  j a  va2s. c  o m
public GcloudPubsub() throws IOException {
    if (CLOUD_PUBSUB_PROJECT_ID == null) {
        throw new IllegalStateException(GCLOUD_PUBSUB_PROJECT_ID_NOT_SET_ERROR);
    }
    try {
        serverName = InetAddress.getLocalHost().getCanonicalHostName();
    } catch (UnknownHostException e) {
        throw new IllegalStateException("Unable to retrieve the hostname of the system");
    }
    HttpTransport httpTransport = checkNotNull(Utils.getDefaultTransport());
    JsonFactory jsonFactory = checkNotNull(Utils.getDefaultJsonFactory());
    GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(PubsubScopes.all());
    }
    HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
    pubsub = new Pubsub.Builder(httpTransport, jsonFactory, initializer).build();
    logger.info("Google Cloud Pub/Sub Initialization SUCCESS");
}

From source file:com.google.cloud.security.scanner.primitives.GCPProject.java

License:Apache License

/**
 * Return the Projects api object used for accessing the Cloud Resource Manager Projects API.
 * @return Projects api object used for accessing the Cloud Resource Manager Projects API
 * @throws GeneralSecurityException Thrown if there's a permissions error.
 * @throws IOException Thrown if there's an IO error initializing the API object.
 *///from ww  w.j  a va 2  s.co  m
public static synchronized Projects getProjectsApiStub() throws GeneralSecurityException, IOException {
    if (projectApiStub != null) {
        return projectApiStub;
    }
    HttpTransport transport;
    GoogleCredential credential;
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    transport = GoogleNetHttpTransport.newTrustedTransport();
    credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
    if (credential.createScopedRequired()) {
        Collection<String> scopes = CloudResourceManagerScopes.all();
        credential = credential.createScoped(scopes);
    }
    projectApiStub = new CloudResourceManager.Builder(transport, jsonFactory, credential).build().projects();
    return projectApiStub;
}

From source file:com.google.cloud.security.scanner.primitives.GCPServiceAccount.java

License:Apache License

/**
 * Get the API stub for accessing the IAM Service Accounts API.
 * @return ServiceAccounts api stub for accessing the IAM Service Accounts API.
 * @throws IOException Thrown if there's an IO error initializing the api connection.
 * @throws GeneralSecurityException Thrown if there's a security error
 * initializing the connection./*  w w  w .j a va2  s.c  om*/
 */
public static ServiceAccounts getServiceAccountsApiStub() throws IOException, GeneralSecurityException {
    if (serviceAccountsApiStub == null) {
        HttpTransport transport;
        GoogleCredential credential;
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        transport = GoogleNetHttpTransport.newTrustedTransport();
        credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
        if (credential.createScopedRequired()) {
            Collection<String> scopes = IamScopes.all();
            credential = credential.createScoped(scopes);
        }
        serviceAccountsApiStub = new Iam.Builder(transport, jsonFactory, credential).build().projects()
                .serviceAccounts();
    }
    return serviceAccountsApiStub;
}

From source file:com.google.cloud.security.scanner.sources.GCSFilesSource.java

License:Apache License

private static Storage constructStorageApiStub() throws GeneralSecurityException, IOException {
    JsonFactory jsonFactory = new JacksonFactory();
    HttpTransport transport;//w w w . ja v a  2s . co  m
    transport = GoogleNetHttpTransport.newTrustedTransport();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
    if (credential.createScopedRequired()) {
        Collection<String> scopes = StorageScopes.all();
        credential = credential.createScoped(scopes);
    }
    return new Storage.Builder(transport, jsonFactory, credential).setApplicationName("GCS Samples").build();
}

From source file:com.google.cloud.servicebroker.awwvision.VisionConfig.java

License:Open Source License

@Bean
Vision vision(HttpTransport transport, JsonFactory jsonFactory, GoogleCredential credential) {
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(VisionScopes.all());
    }//from   ww  w  .jav  a  2s. c  om
    return new Vision.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build();
}

From source file:com.google.cloud.servicebroker.awwvision.VisionConfig.java

License:Open Source License

@Bean
Storage storage(HttpTransport transport, JsonFactory jsonFactory, GoogleCredential credential) {
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(StorageScopes.all());
    }/*from  w  w  w.j  a v  a 2s. co  m*/
    return new Storage.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build();
}

From source file:com.google.cloud.sparkdemo.BigqueryWriter.java

License:Apache License

private Bigquery createAuthorizedClient() {
    try {/*  www.  ja v  a2  s.  co  m*/
        // Create the credential
        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);
        }

        return new Bigquery.Builder(transport, jsonFactory, credential)
                .setApplicationName("Spark Gaming Samples").build();
    } catch (IOException e) {
        // can not create bigquery client
        e.printStackTrace();
        return null;
    }
}

From source file:com.google.cloud.sparkdemo.CloudPubsubReceiver.java

License:Apache License

private Pubsub createAuthorizedClient() {
    try {/*from  w ww  . ja  v  a2  s . com*/
        // Create the credential
        HttpTransport httpTransport = Utils.getDefaultTransport();
        JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
        GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);

        if (credential.createScopedRequired()) {
            credential = credential.createScoped(PubsubScopes.all());
        }
        HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
        return new Pubsub.Builder(httpTransport, jsonFactory, initializer)
                .setApplicationName("spark-pubsub-receiver").build();
    } catch (IOException e) {
        reportError("Unable to create Cloud Pub/sub client.", e);
        return null;
    }
}

From source file:com.google.cloud.storage.storagetransfer.samples.TransferClientCreator.java

License:Open Source License

/**
 * Create a Storage Transfer client using user-supplied credentials and other settings.
 *
 * @param httpTransport/*from  w w  w  .  ja va2 s . c o  m*/
 *          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,
        GoogleCredential 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 RetryHttpInitializerWrapper(credential);
    return new Storagetransfer.Builder(httpTransport, jsonFactory, initializer)
            .setApplicationName("storagetransfer-sample").build();
}