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:domain.bsu.dektiarev.service.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);*/
    GoogleCredential credential = GoogleCredential
            .fromStream(new FileInputStream("resources/APNewsFeed-7e0946ab6fb1.json"))
            .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_READ_WRITE));
    if (credential.createScopedRequired()) {
        Collection<String> bigqueryScopes = StorageScopes.all();
        credential = credential.createScoped(bigqueryScopes);
    }/*from   w  w w.  java  2 s .  com*/

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

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

License:Open Source License

public static void run() throws Exception {
    parseParams();//  www.  j ava2 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.export.sheet.SpreadsheetServiceModule.java

License:Open Source License

@Provides
static SpreadsheetService provideSpreadsheetService(GoogleCredential credential) {
    SpreadsheetService service = new SpreadsheetService(APPLICATION_NAME);
    service.setOAuth2Credentials(credential.createScoped(SCOPES));
    return service;
}

From source file:google.registry.groups.DirectoryModule.java

License:Open Source License

@Provides
static Directory provideDirectory(@Named("delegatedAdmin") GoogleCredential credential,
        @Config("projectId") String projectId) {
    return new Directory.Builder(credential.getTransport(), credential.getJsonFactory(),
            credential.createScoped(ImmutableSet.of(DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER,
                    DirectoryScopes.ADMIN_DIRECTORY_GROUP))).setApplicationName(projectId).build();
}

From source file:google.registry.groups.GroupssettingsModule.java

License:Open Source License

@Provides
static Groupssettings provideDirectory(@Named("delegatedAdmin") GoogleCredential credential,
        @Config("projectId") String projectId) {
    return new Groupssettings.Builder(credential.getTransport(), credential.getJsonFactory(),
            credential.createScoped(ImmutableSet.of(GroupssettingsScopes.APPS_GROUPS_SETTINGS)))
                    .setApplicationName(projectId).build();
}

From source file:imageSearching.DetectImage.java

License:Open Source License

/**
 * Connects to the Vision API using Application Default Credentials.
 *//*from w  w  w .  j  a  va 2  s  .  c  om*/
public static Vision getVisionService() throws IOException, GeneralSecurityException {
    File credentialsFile = new File("license/license.txt");
    InputStream credentialsStream = new FileInputStream(credentialsFile);

    GoogleCredential credential = GoogleCredential.fromStream(credentialsStream);
    credential = credential.createScoped(VisionScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential)
            .setApplicationName(APPLICATION_NAME).build();
}

From source file:io.druid.storage.google.GoogleStorageDruidModule.java

License:Apache License

@Provides
@LazySingleton//from   ww w .  jav  a  2  s .c  om
public GoogleStorage getGoogleStorage(final GoogleAccountConfig config)
        throws IOException, GeneralSecurityException {
    LOG.info("Building Cloud Storage Client...");

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

    GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(StorageScopes.all());
    }
    Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential)
            .setApplicationName(APPLICATION_NAME).build();

    return new GoogleStorage(storage);
}

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

License:Apache License

public SQLAdmin sqlAdmin() {
    if (sqlAdmin == null) {
        try {/*from   ww w  . j  a va2s .  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  w  w. j a v  a 2  s . 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.
 *//*  w ww  . j  a  va 2s. 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;
}