Example usage for com.google.api.client.googleapis.javanet GoogleNetHttpTransport newTrustedTransport

List of usage examples for com.google.api.client.googleapis.javanet GoogleNetHttpTransport newTrustedTransport

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.javanet GoogleNetHttpTransport newTrustedTransport.

Prototype

public static NetHttpTransport newTrustedTransport() throws GeneralSecurityException, IOException 

Source Link

Document

Returns a new instance of NetHttpTransport that uses GoogleUtils#getCertificateTrustStore() for the trusted certificates using com.google.api.client.http.javanet.NetHttpTransport.Builder#trustCertificates(KeyStore) .

Usage

From source file:sentiment.Analyze.java

License:Open Source License

/**
 * Connects to the Natural Language API using Application Default
 * Credentials.// ww  w  . j ava 2  s.c  om
 */
public static CloudNaturalLanguageAPI getLanguageService() throws IOException, GeneralSecurityException {

    GoogleCredential credential = GoogleCredential
            .fromStream(Analyze.class.getResourceAsStream("googlecredential.json"))
            .createScoped(CloudNaturalLanguageAPIScopes.all());
    //                = 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:step.datapool.gsheet.GoogleSheetv4DataPool.java

License:Open Source License

public Sheets buildService(GoogleCredential credential) throws IOException, GeneralSecurityException {
    return new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(),
            credential).setApplicationName("Sheets API Snippets").build();
}

From source file:swp.bibjsf.isbnsearch.ISBNGoogleSearch.java

License:Apache License

/**
 * Sets up the book client retrieving the API key and setting up the connect.
 *
 * @param jsonFactory the JSON factory required to transfer objects
 * @return the book search client connection
 * @throws GeneralSecurityException in case the API cannot be accessed
 * @throws IOException in case of connection failure
 *///from  w ww. j  a  va 2  s  . com
protected static Books setupBookClient(final JsonFactory jsonFactory)
        throws GeneralSecurityException, IOException {
    final String apiKey = GoogleAPIKey.getKey();

    // Set up Books client.
    final Books books = new Books.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, null)
            .setApplicationName(APPLICATION_NAME)
            .setGoogleClientRequestInitializer(new BooksRequestInitializer(apiKey)).build();
    return books;
}

From source file:sync.GoogleCalendar.java

License:Apache License

public GoogleCalendar() {
    try {//from  www  .  j a va2s.  com
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        Credential credential = authorize(httpTransport, dataStoreFactory, JSON_FACTORY);

        // set up global Calendar instance
        client = new com.google.api.services.calendar.Calendar.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();

        CalendarList feed = client.calendarList().list().execute();
        bimsbCal = getBIMSBCalendar(feed);

        if (bimsbCal != null)
            System.out.println("Found: " + bimsbCal.getSummary() + " [" + bimsbCal.getDescription() + "]");
        else
            throw new RuntimeException("Could not find BIMSB Calendar.");
    } catch (Exception e) {
        throw new RuntimeException("An error occured: " + e);
    }
}

From source file:tk.feelai.bigquery.CsvUploader.java

License:Apache License

public CsvUploader() throws Exception {
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
}

From source file:tv.bioscope.taskqueue.TaskQueueWorker.java

License:Apache License

/**
* You can perform following operations using TaskQueueService:
* <ul>//from  ww w . j  av a 2s. c om
* <li>leasetasks</li>
* <li>gettask</li>
* <li>deletetask</li>
* <li>getqueue</li>
* </ul>
* <p>
* For illustration purpose, we are first getting the stats of the specified queue followed by
* leasing tasks and then deleting them. Users can change the flow according to their needs.
* </p>
*/
private static void run() throws Exception {

    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
    // authorization
    Credential credential = authorize();

    initializeDatastore();

    // set up Taskqueue
    final Taskqueue taskQueue = new Taskqueue.Builder(httpTransport, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .setTaskqueueRequestInitializer(new TaskqueueRequestInitializer() {
                @Override
                public void initializeTaskqueueRequest(TaskqueueRequest<?> request) {
                    request.setPrettyPrint(true);
                }
            }).build();

    // get queue
    com.google.api.services.taskqueue.model.TaskQueue queue = getQueue(taskQueue);
    System.out.println(queue);

    // Keep polling for tasks and process them as they come in.
    while (true) {
        // lease, execute and delete tasks
        Tasks tasks = getLeasedTasks(taskQueue);
        if (tasks == null || tasks.getItems() == null || tasks.getItems().size() == 0) {
            System.out.println("No tasks to lease. Sleeping..");
            Thread.sleep(1000);
            continue;
        }

        for (final Task leasedTask : tasks.getItems()) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        executeTask(leasedTask);
                        deleteTask(taskQueue, leasedTask);
                    } catch (Exception e) {
                        System.out.println("Failed to execute / delete task.. ");
                    }
                }
            });
        }
    }
}

From source file:ubicrypt.core.provider.gdrive.GDriveAuthorizer.java

License:Open Source License

public String authorizeUrl() throws GeneralSecurityException, IOException {
    if (!status.compareAndSet(false, true)) {
        throw new IllegalStateException("authorization in progress");
    }/*from www . ja v  a 2s .c o m*/
    try {
        conf = new GDriveConf(credentialId, this);
        http = GoogleNetHttpTransport.newTrustedTransport();
        flow = new GoogleAuthorizationCodeFlow.Builder(http, getDefaultInstance(), appCredentials(),
                Arrays.asList(DriveScopes.DRIVE_FILE)).setDataStoreFactory(this).setAccessType("offline")
                        .build();
        receiver = new LocalServerReceiver();
        redirectUri = receiver.getRedirectUri();
        return flow.newAuthorizationUrl().setRedirectUri(redirectUri).build();
    } catch (Exception e) {
        close();
        status.set(false);
        Throwables.propagate(e);
        return null;
    }
}

From source file:uk.co.inetria.pi.app.CmdLineAuthenticationProvider.java

License:Apache License

/**
 * Create an instance of Credential//from w w w  . ja  va2s.  c o m
 * @return
 * @throws IOException
 */
protected Credential getCredential() throws IOException {

    if (this.clientSecretsFile == null) {
        throw new IllegalArgumentException("client secrets file is required");
    }

    if (this.scopes == null) {
        throw new IllegalArgumentException("you need to provide at least one scope");
    }

    // initialize the transport
    try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    } catch (GeneralSecurityException e) {
        log.log(Level.SEVERE, "failed to create transport", e);
        throw new IOException(e);
    }

    // initialize the data store factory
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(getClass().getClassLoader().getResourceAsStream(this.clientSecretsFile)));

    // Set up authorization code flow.

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
            clientSecrets, scopes).setDataStoreFactory(dataStoreFactory).build();

    // authorize, this will prompt the user to enter a url on their browser then paste the code 
    return new AuthorizationCodeInstalledApp(flow, new PromptReceiver()).authorize("user");

    // the line below will expect you to have some way to browse to the URL locally, not useful when you 
    // are running a Pi through SSH
    // return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:w3bigdata.downloader.DriveSample.java

License:Apache License

public static void main(String[] args) {
    try {/*  w ww. java  2 s  .c om*/
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        // authorization
        Credential credential = authorize();
        // set up the global Drive instance
        drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME)
                .build();
        printFilesInFolder(drive, "0B4o7m0XMEBWuUUxMRGhIeUQxVTA");
        // run commands
        View.header1("Success!");
        return;
    } catch (IOException e) {
        System.err.println(e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.exit(1);
}

From source file:wmg.datagrabber.google.youtubegrabber.cmdline.YouTubeGrabber.java

License:Apache License

public void instantiateYouTube() {
    try {/*www. j ava2s  . co m*/
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    authorize();
    youtube = new YouTube.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME)
            .build();
}