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() throws IOException 

Source Link

Document

Beta
Returns the Application Default Credentials.

Usage

From source file:com.netflix.spinnaker.halyard.core.registry.v1.GoogleWriteableProfileRegistry.java

License:Apache License

private GoogleCredential loadCredential(HttpTransport transport, JsonFactory factory, String jsonPath)
        throws IOException {
    GoogleCredential credential;/*from w  w w.  jav  a2s. c o  m*/
    if (!jsonPath.isEmpty()) {
        FileInputStream stream = new FileInputStream(jsonPath);
        credential = GoogleCredential.fromStream(stream, transport, factory)
                .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL));
        log.info("Loaded credentials from " + jsonPath);
    } else {
        log.info("Using default application credentials.");
        credential = GoogleCredential.getApplicationDefault();
    }
    return credential;
}

From source file:com.netflix.spinnaker.igor.gcb.GoogleCredentialService.java

License:Apache License

GoogleCredential getApplicationDefault() {
    try {// w ww  .  jav  a  2  s .com
        return GoogleCredential.getApplicationDefault();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.spotify.styx.client.auth.GoogleIdTokenAuth.java

License:Apache License

public static GoogleIdTokenAuth ofDefaultCredential() {
    try {//from  w  w  w  .j  av a2s.c o m
        return of(Optional.of(GoogleCredential.getApplicationDefault()));
    } catch (IOException e) {
        return of(Optional.empty());
    }
}

From source file:com.twitter.heron.uploader.gcs.GcsUploader.java

License:Open Source License

private Credential createCredentials(Config configuration) throws IOException {
    final String credentialsPath = GcsContext.getCredentialsPath(configuration);
    if (!Strings.isNullOrEmpty(credentialsPath)) {
        LOG.info("Using credentials from file: " + credentialsPath);
        return GoogleCredential.fromStream(new FileInputStream(credentialsPath))
                .createScoped(StorageScopes.all());
    }/*from  w  ww.j a v  a2 s  . c o  m*/

    // if a credentials path is not provided try using the application default one.
    LOG.info("Using default application credentials");
    return GoogleCredential.getApplicationDefault().createScoped(StorageScopes.all());
}

From source file:fi.gapps.intra.thesis.controller.TaskQueueSample.java

License:Open Source License

public static void run() throws Exception {
    parseParams();//from  ww  w  .j a v  a2  s  .c om
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    // [START auth_and_intitalize_client]
    // Authenticate using Google Application Default Credentials.
    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    if (credential.createScopedRequired()) {
        List<String> scopes = new ArrayList<>();
        // Set TaskQueue Scopes
        scopes.add(TaskqueueScopes.TASKQUEUE);
        scopes.add(TaskqueueScopes.TASKQUEUE_CONSUMER);
        credential = credential.createScoped(scopes);
    }
    // Intialize Taskqueue object.
    Taskqueue taskQueue = new Taskqueue.Builder(httpTransport, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME).build();
    // [END auth_and_intitalize_client]

    /*
     * Get the task queue using the name of an existing task queue listed in
     * the App Engine Task Queue section of the Developers console. See the
     * following sample for an example App Engine app that creates a pull
     * task queue:
     * https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/
     * appengine/taskqueue
     */
    com.google.api.services.taskqueue.model.TaskQueue queue = getQueue(taskQueue);
    System.out.println("================== Listing Task Queue details ==================");
    System.out.println(queue);
    // Lease, process and delete tasks
    // [START lease_tasks]
    Tasks tasks = getLeasedTasks(taskQueue);
    if (tasks.getItems() == null || tasks.getItems().size() == 0) {
        System.out.println("No tasks to lease, so now exiting");
    } else {
        for (Task leasedTask : tasks.getItems()) {
            processTask(leasedTask);
            deleteTask(taskQueue, leasedTask);
        }
    }
    // [END lease_tasks]
}

From source file:google.registry.monitoring.metrics.example.SheepCounterExample.java

License:Open Source License

private static Monitoring createAuthorizedMonitoringClient() throws IOException {
    // Grab the Application Default Credentials from the environment.
    // Generate these with 'gcloud beta auth application-default login'
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(MonitoringScopes.all());

    // Create and return the CloudMonitoring service object
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    return new Monitoring.Builder(httpTransport, jsonFactory, credential)
            .setApplicationName("Monitoring Sample").build();
}

From source file:it.noovle.dataflow.TwitterProcessor.java

License:Open Source License

/**
 * Connects to the Natural Language API using Application Default Credentials.
 *///from  ww w.  j  ava 2  s .  c o  m
private static CloudNaturalLanguageAPI getLanguageService() throws IOException, GeneralSecurityException {
    final GoogleCredential credential = GoogleCredential.getApplicationDefault()
            .createScoped(CloudNaturalLanguageAPIScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    return new CloudNaturalLanguageAPI.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            new HttpRequestInitializer() {
                @Override
                public void initialize(HttpRequest request) throws IOException {
                    credential.initialize(request);
                }
            }).setApplicationName(APPLICATION_NAME).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 ww.  j  a  va 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;
}