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:com.netflix.spinnaker.igor.config.GoogleCloudBuildConfig.java

License:Apache License

@Bean
HttpTransport httpTransport() throws IOException, GeneralSecurityException {
    return GoogleNetHttpTransport.newTrustedTransport();
}

From source file:com.nokia.scbe.bestdayever.calendar.CalendarSample.java

License:Apache License

public static void main(String[] args) {
    try {//w  w  w  .j  a  v  a  2  s .c o  m
        try {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            // authorization
            Credential credential = authorize();

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

            /////////////
            CalendarList calendarList = showCalendars();
            for (CalendarListEntry calendarEntry : calendarList.getItems()) {
                Events feed = client.events().list(calendarEntry.getId()).execute();
                //           System.out.println(calendarEntry.getId());
                View.display(feed);

            }
            /////////////

            //        addCalendarsUsingBatch();
            //        Calendar calendar = addCalendar();
            //        updateCalendar(calendar);
            //        addEvent(calendar);
            //        showEvents(calendar);
            //        deleteCalendarsUsingBatch();
            //        deleteCalendar(calendar);

        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.exit(1);
}

From source file:com.nokia.scbe.bestdayever.calendar.FreeBusyTimesRetriever.java

License:Apache License

private void initialize() {
    try {//from   w  w w .  ja va2  s  . c om
        HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

        Credential credential = authorize();

        assert (credential != null);

        service = new com.google.api.services.calendar.Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY,
                credential).setApplicationName(APPLICATION_NAME).build();

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Exception in constructor: " + e);
    }
}

From source file:com.northernwall.hadrian.calendar.google.GoogleCalendarHelperFactory.java

License:Apache License

@Override
public CalendarHelper create(Parameters parameters, OkHttpClient client) {
    try {/*from  w ww.jav a 2 s . co  m*/
        String appName = parameters.getString(Const.CALENDAR_GOOGLE_APP_NAME, "service-delivery-tool");

        String accountId = parameters.getString(Const.CALENDAR_GOOGLE_ACCOUNT_ID, null);
        if (accountId == null || accountId.isEmpty()) {
            logger.error("{} can not be null or empty", Const.CALENDAR_GOOGLE_ACCOUNT_ID);
            return new SimpleCalendarHelper();
        }

        String privateKeyId = parameters.getString(Const.CALENDAR_GOOGLE_PRIVATE_KEY_ID, null);
        if (privateKeyId == null || privateKeyId.isEmpty()) {
            logger.error("{} can not be null or empty", Const.CALENDAR_GOOGLE_PRIVATE_KEY_ID);
            return new SimpleCalendarHelper();
        }

        PrivateKey privateKey = getPemPrivateKey(parameters);
        if (privateKey == null) {
            logger.error("{} can not be null or empty", Const.CALENDAR_GOOGLE_PEM_FILE);
            return new SimpleCalendarHelper();
        }

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

        // load client secrets
        GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                .setJsonFactory(jsonFactory).setServiceAccountId(accountId)
                .setServiceAccountPrivateKeyId(privateKeyId).setServiceAccountPrivateKey(privateKey)
                .setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/calendar"))
                .build();

        //build calendar client
        Calendar calendarClient = new Builder(httpTransport, jsonFactory, credential)
                .setApplicationName(appName).build();
        logger.info("Finished building GoogleCalendarHelper successfully");
        return new GoogleCalendarHelper(calendarClient, parameters);
    } catch (IOException ex) {
        logger.error("IO Exception while building GoogleCalendarHelper", ex);
        return new SimpleCalendarHelper();
    } catch (GeneralSecurityException ex) {
        logger.error("General Security Exception while building GoogleCalendarHelper", ex);
        return new SimpleCalendarHelper();
    }
}

From source file:com.omertron.slackbot.functions.GoogleSheets.java

License:Open Source License

/**
 * Creates an authorised Credential object.<p>
 * Build and return an authorised Sheets API client service.
 *
 *///from  ww  w .j  a  va  2  s  . c  o  m
public static void initialise() {
    if (credential == null) {
        LOG.info("Attempting to authorise");
        try {
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            credential = GoogleCredential.fromStream(new FileInputStream("SlackBggBot-7a8afe5ba1eb.json"))
                    .createScoped(Arrays.asList(SheetsScopes.SPREADSHEETS));
        } catch (IOException | GeneralSecurityException ex) {
            LOG.warn("Failed to authorise: {}", ex.getMessage(), ex);
        }
    }
    LOG.info("Authorised!");

    if (sheets == null) {
        LOG.info("Attempting to get sheet service");
        sheets = new Sheets.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(Constants.BOT_NAME).build();
    }

    LOG.info("Got sheet service");

}

From source file:com.otway.picasasync.webclient.GoogleOAuth.java

License:Apache License

public PicasawebClient authenticatePicasa(Settings settings, boolean allowInteractive, SyncState state)
        throws IOException, GeneralSecurityException {
    final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    log.info("Preparing to authenticate via OAuth...");
    Credential cred = null;// w ww .j a v  a2s  . com

    String refreshToken = settings.getRefreshToken();
    if (refreshToken != null) {
        // We have a refresh token - so get some refreshed credentials
        cred = getRefreshedCredentials(refreshToken);
    }

    if (cred == null && allowInteractive) {

        // Either there was no valid refresh token, or the credentials could not
        // be created (they may have been revoked). So run the auth flow

        log.info("No credentials - beginning OAuth flow...");

        state.setStatus("Requesting Google Authentication...");

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(httpTransport, jsonFactory, clientId,
                clientSecret, Collections.singleton(scope));

        String authorizationUrl = flow.newAuthorizationUrl().setRedirectUri(redirectUrl)
                .setAccessType("offline").setApprovalPrompt("force").build();

        try {
            OAuthGUI authGUI = new OAuthGUI();

            // Display the interactive GUI for the user to log in via the browser
            String code = authGUI.initAndShowGUI(authorizationUrl, state);

            log.info("Token received from UI. Requesting credentials...");

            // Now we have the code from the interactive login, set up the
            // credentials request and call it.
            GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUrl).execute();

            // Retrieve the credential from the request response
            cred = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory)
                    .setClientSecrets(clientId, clientSecret).build().setFromTokenResponse(response);

            state.setStatus("Google Authentication succeeded.");

            log.info("Credentials received - storing refresh token...");

            // Squirrel this away for next time
            settings.setRefreshToken(cred.getRefreshToken());
            settings.saveSettings();
        } catch (Exception ex) {
            log.error("Failed to initialise interactive OAuth GUI", ex);
        }
    }

    if (cred != null) {

        log.info("Building PicasaWeb Client...");

        // Build a web client using the credentials we created
        return new PicasawebClient(cred);
    }

    return null;
}

From source file:com.otway.picasasync.webclient.GoogleOAuth.java

License:Apache License

public Credential getRefreshedCredentials(String refreshCode) throws IOException, GeneralSecurityException {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    log.info("Getting access token for refresh token..");

    try {// ww w.  j  a va 2 s.  c o  m
        GoogleTokenResponse response = new GoogleRefreshTokenRequest(httpTransport, jsonFactory, refreshCode,
                clientId, clientSecret).execute();

        return new GoogleCredential().setAccessToken(response.getAccessToken());

    } catch (UnknownHostException ex) {
        log.error("Unknown host. No web access?");
        throw ex;
    } catch (IOException e) {
        log.error("Exception getting refreshed auth: ", e);
    }
    return null;
}

From source file:com.rabadancm.gtm.api.v2.AuthenticatorJson.java

public TagManager getGtmObject() {

    TagManager gtm = null;/*w  w  w.  ja  va2  s.  co m*/
    Credential credential = null;
    try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

        // Authorization flow.
        credential = authorize();
        gtm = new TagManager.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return gtm;
}

From source file:com.rabadancm.gtm.api.v2.AuthenticatorP12.java

public TagManager getGtmObject() {
    TagManager gtm = null;/*from w  ww  .  ja  v  a 2  s . c o m*/
    try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        // Authorization flow.
        Credential credential = authorize();
        gtm = new TagManager.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return gtm;
}

From source file:com.rabadancm.gtm.api.v2.AuthenticatorP12.java

private Credential authorize() throws Exception {

    // Construct a GoogleCredential object with the service account email
    // and p12 file downloaded from the developer console.
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountPrivateKeyFromP12File(new File(SERVICE_ACCOUNT_PRIVATE_KEY))
            .setServiceAccountScopes(TagManagerScopes.all()).build();

    return credential;
}