List of usage examples for com.google.api.client.googleapis.javanet GoogleNetHttpTransport newTrustedTransport
public static NetHttpTransport newTrustedTransport() throws GeneralSecurityException, IOException
From source file:com.sappe.ontrack.security.OAuth2Example.java
License:Apache License
public static void main(String[] args) { try {// w ww . ja va 2s . c o m httpTransport = GoogleNetHttpTransport.newTrustedTransport(); dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // authorization Credential credential = authorize(); // set up global Oauth2 instance oauth2 = new Oauth2.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build(); // run commands tokenInfo(credential.getAccessToken()); userInfo(); // success! return; } catch (IOException e) { System.err.println(e.getMessage()); } catch (Throwable t) { t.printStackTrace(); } System.exit(1); }
From source file:com.spotify.styx.StyxScheduler.java
License:Apache License
private static Container createGkeClient() { try {// www .j av a 2 s .com final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); final JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); final GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory) .createScoped(ContainerScopes.all()); return new Container.Builder(httpTransport, jsonFactory, credential).setApplicationName(SERVICE_NAME) .build(); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException(e); } }
From source file:com.spotify.styx.StyxScheduler.java
License:Apache License
private static ServiceAccountKeyManager createServiceAccountKeyManager() { try {//w w w . ja v a 2 s .co m final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); final JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); final GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory) .createScoped(IamScopes.all()); final Iam iam = new Iam.Builder(httpTransport, jsonFactory, credential).setApplicationName(SERVICE_NAME) .build(); return new ServiceAccountKeyManager(iam); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException(e); } }
From source file:com.swingtech.apps.filemgmt.util.GoogleApiUtils.java
License:Open Source License
private static void initGoogleApi(java.io.File dataStoreDirectory) { try {/*from ww w . j av a2 s . c o m*/ if (HTTP_TRANSPORT == null) { HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); } if (DATA_STORE_FACTORY == null) { DATA_STORE_FACTORY = new FileDataStoreFactory(dataStoreDirectory); } } catch (Throwable t) { throw new RuntimeException("Error Initializing Google API. dataStoreDirectory: '" + dataStoreDirectory.getAbsolutePath() + "'", t); } }
From source file:com.tango.BucketSyncer.StorageClients.GCSClient.java
License:Apache License
public void createClient(MirrorOptions options) throws IllegalStateException, IllegalArgumentException { //check Application Name if (options.getGCS_APPLICATION_NAME() == null) { log.error("Please provide application name for GCS"); throw new IllegalArgumentException("Please provide application name for GCS"); }//from w w w . j a va 2s . c o m try { gcsHttpTransport = GoogleNetHttpTransport.newTrustedTransport(); gcsDataStoreFactory = new FileDataStoreFactory(GCS_DATA_STORE_DIR); // authorization, check credentials Credential gcsCredential = null; try { gcsCredential = gcsAuthorize(); } catch (IllegalArgumentException e) { log.error("Please provide valid GCS credentials"); Throwables.propagate(e); } catch (Exception e) { log.error("Failed to create GCS client. Credentials for GCS maybe invalid: {}", e); throw new IllegalStateException("Failed to create GCS client. Credentials for GCS maybe invalid."); } // set up global Storage instance gcsClient = new Storage.Builder(gcsHttpTransport, GCS_JSON_FACTORY, gcsCredential) .setApplicationName(options.getGCS_APPLICATION_NAME()).build(); } catch (GeneralSecurityException e) { log.error("Failed to create GCS client. Client_secret maybe invalid: {}", e); throw new IllegalStateException("Failed to create GCS client. Client_secret maybe invalid."); } catch (IOException e) { log.error("Failed to create GCS client: {}", e); throw new IllegalStateException("Failed to create GCS client."); } }
From source file:com.tfgridiron.crowdsource.cmdline.GameCharter.java
License:Apache License
private static void authorize() throws Exception { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // load client secrets InputStream inputStream = GameCharter.class.getResourceAsStream("/client_secrets.json"); if (inputStream == null) { inputStream = GameCharter.class.getResourceAsStream("client_secrets.json"); if (inputStream == null) { inputStream = GameCharter.class.getResourceAsStream("resources/client_secrets.json"); if (inputStream == null) { inputStream = GameCharter.class.getResourceAsStream("/resources/client_secrets.json"); }/* ww w. j a v a 2 s.c om*/ } } if (inputStream == null) { throw new IOException("Could not get client_secrets.json from ... anywhere"); } GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inputStream)); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { System.err.println("Internal erorr: Enter Client ID and Secret from " + "https://code.google.com/apis/console/?api=drive into client_secrets.json"); System.exit(1); } Set<String> scopes = new HashSet<String>(2); scopes.add(DriveScopes.DRIVE); scopes.add(Constants.OAUTH_SPREADSHEET_SCOPE); // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, scopes).setDataStoreFactory(dataStoreFactory).build(); // authorize credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); }
From source file:com.thesoftwareguild.flightmaster.queryProcessor.QPXFlightQuery.java
/** * * @return @throws IOException/*from w w w . j a va 2s . co m*/ */ @Override public List<Flight> execute() throws IOException { //FlightQueryResult> resultList = new ArrayList<>(); List<Flight> retList = new ArrayList<>(); try { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); QPXExpress qpXExpress = new QPXExpress.Builder(httpTransport, JSON_FACTORY, null) .setApplicationName(APPLICATION_NAME) .setGoogleClientRequestInitializer(new QPXExpressRequestInitializer(API_KEY)).build(); PassengerCounts passengers = new PassengerCounts(); passengers.setAdultCount(adultPassengers); passengers.setChildCount(childPassengers); passengers.setSeniorCount(seniorPassengers); passengers.setInfantInSeatCount(infantInSeatCount); List<SliceInput> slices = new ArrayList<>(); // Flight to destination SliceInput departSlice = new SliceInput(); departSlice.setOrigin(origin); departSlice.setDestination(destination); departSlice.setDate(df.format(departDate)); departSlice.setMaxStops(maxStops); slices.add(departSlice); // Flight back home if it exists if (returnDate != null) { SliceInput returnSlice = new SliceInput(); returnSlice.setOrigin(destination); returnSlice.setDestination(origin); returnSlice.setDate(df.format(returnDate)); returnSlice.setMaxStops(maxStops); slices.add(returnSlice); } TripOptionsRequest request = new TripOptionsRequest(); request.setSolutions(MAX_FLIGHTS_RETURNED); // number of flights to return request.setPassengers(passengers); request.setSlice(slices); TripsSearchRequest parameters = new TripsSearchRequest(); parameters.setRequest(request); TripsSearchResponse list = qpXExpress.trips().search(parameters).execute(); // executes the search List<TripOption> tripResults = list.getTrips().getTripOption(); for (int i = 0; i < tripResults.size(); i++) { Flight flight = new Flight(); flight.setRequestId(requestId); flight.setOrigin(origin); flight.setDestination(destination); flight.setQueryTime(new Date(System.currentTimeMillis())); TripOption tripResult = tripResults.get(i); flight.setFightId(tripResult.getId()); Integer totalDuration = 0; flight.setPrice(Double.parseDouble(tripResult.getSaleTotal().replaceAll("[a-zA-Z]", ""))); for (int j = 0; j < tripResult.getSlice().size(); j++) { List<SegmentInfo> segment = tripResult.getSlice().get(j).getSegment(); totalDuration += tripResult.getSlice().get(j).getDuration(); for (SegmentInfo segment1 : segment) { flight.addFlightLeg(segment1.getBookingCode(), segment1.getFlight().getNumber(), segment1.getFlight().getCarrier()); flight.setCarrier(segment1.getFlight().getCarrier()); } } flight.setDuration(totalDuration); retList.add(flight); } } catch (GeneralSecurityException ex) { logger.error(ex); } // Reset all parameters requestId = 0; adultPassengers = 0; childPassengers = 0; seniorPassengers = 0; infantInSeatCount = 0; maxStops = 0; origin = null; destination = null; departDate = null; returnDate = null; return retList; }
From source file:com.twitter.heron.uploader.gcs.GcsUploader.java
License:Open Source License
private Storage createStorage(Credential credential) throws GeneralSecurityException, IOException { final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new Storage.Builder(httpTransport, jsonFactory, credential).build(); }
From source file:com.vmware.photon.controller.model.adapters.gcp.GCPTestUtil.java
License:Open Source License
/** * Get a Google Compute Engine client object. * @param userEmail The service account's client email. * @param privateKey The service account's private key. * @param scopes The scopes used in the compute client object. * @param applicationName The application name. * @return The created Compute Engine client object. * @throws GeneralSecurityException Exception when creating http transport. * @throws IOException Exception when creating http transport. *///from w w w . j av a 2s . co m public static Compute getGoogleComputeClient(String userEmail, String privateKey, List<String> scopes, String applicationName) throws GeneralSecurityException, IOException { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport) .setJsonFactory(JSON_FACTORY).setServiceAccountId(userEmail).setServiceAccountScopes(scopes) .setServiceAccountPrivateKey(privateKeyFromPkcs8(privateKey)).build(); return new Compute.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(applicationName) .build(); }
From source file:controllers.PlusSample.java
License:Apache License
public static void salah() { try {//from www .jav a 2s .co m System.out.println("TAWADLAH"); httpTransport = GoogleNetHttpTransport.newTrustedTransport(); dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // authorization Credential credential = authorize(); // set up global Plus instance Drive service = new Drive.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build(); List<File> result = new ArrayList<File>(); com.google.api.services.drive.Drive.Files.List request = service.files().list(); do { try { FileList files = request.execute(); System.out.print(files.toString()); result.addAll(files.getItems()); request.setPageToken(files.getNextPageToken()); } catch (IOException e) { System.out.println("An error occurred: " + e); request.setPageToken(null); } } while (request.getPageToken() != null && request.getPageToken().length() > 0); // run commands // success! return; } catch (IOException e) { System.err.println(e.getMessage()); } catch (Throwable t) { t.printStackTrace(); } System.exit(1); }