List of usage examples for com.google.api.client.googleapis.javanet GoogleNetHttpTransport newTrustedTransport
public static NetHttpTransport newTrustedTransport() throws GeneralSecurityException, IOException
From source file:com.examples.abelanav2.storage.CloudStorage.java
License:Open Source License
/** * Returns a Google Cloud Storage upload url. * * @param name the filename//from www . j av a2 s . co m * @return a URL */ public static String getUploadUrl(final String name) { try { GoogleCredential googleCredentials = GoogleCredential.getApplicationDefault() .createScoped(Collections.singleton(STORAGE_SCOPE)); String uri = "https://www.googleapis.com/upload/storage/v1/b/" + URLEncoder.encode(BackendConstants.UPLOAD_BUCKET_NAME, "UTF-8") + "/o?uploadType=resumable&name=" + URLEncoder.encode(name, "UTF-8"); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); HttpRequestFactory requestFactory = httpTransport.createRequestFactory(googleCredentials); GenericUrl url = new GenericUrl(uri); HttpRequest request = requestFactory.buildPostRequest(url, new EmptyContent()); try { HttpResponse response = request.execute(); if (response.getStatusCode() == HttpStatusCodes.STATUS_CODE_OK) { return response.getHeaders().getLocation(); } else { LOGGER.severe("Could not get upload URL, HTTP code = " + response.getStatusCode()); } } catch (IOException e) { LOGGER.severe("API request to Google Cloud Storage failed: " + e.getMessage()); } } catch (IOException e) { LOGGER.severe("Could not get credentials: " + e.getMessage()); } catch (GeneralSecurityException e) { LOGGER.severe("Could not start the transport layer for upload: " + e.getMessage()); } return null; }
From source file:com.examples.abelanav2.TaskQueueNotificationServlet.java
License:Open Source License
@Override public final void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { HttpTransport httpTransport;// w w w. ja v a 2 s . c o m try { Map<Object, Object> params = new HashMap<>(); params.putAll(req.getParameterMap()); params.put("task", req.getHeader("X-AppEngine-TaskName")); httpTransport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = GoogleCredential.getApplicationDefault() .createScoped(Collections.singleton("https://www.googleapis.com/auth/userinfo.email")); HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); GenericUrl url = new GenericUrl(ConfigurationConstants.IMAGE_RESIZER_URL); HttpRequest request = requestFactory.buildPostRequest(url, new UrlEncodedContent(params)); credential.initialize(request); HttpResponse response = request.execute(); if (!response.isSuccessStatusCode()) { log("Call to the imageresizer failed: " + response.getContent().toString()); resp.setStatus(HttpStatusCodes.STATUS_CODE_SERVER_ERROR); } else { resp.setStatus(response.getStatusCode()); } } catch (GeneralSecurityException | IOException e) { log("Http request error: " + e.getMessage()); resp.setStatus(HttpStatusCodes.STATUS_CODE_SERVER_ERROR); } }
From source file:com.freesundance.contacts.google.ContactsExample.java
License:Apache License
private ContactsService authenticate() throws GeneralSecurityException, IOException, ServiceException { JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); String APPLICATION_NAME = "juppfamily-contacts-1"; String SERVICE_ACCOUNT_EMAIL = "account-1@our-contacts-1136.iam.gserviceaccount.com"; String accountUser = "nostro@juppfamily.info"; GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport) .setJsonFactory(jsonFactory).setServiceAccountId(SERVICE_ACCOUNT_EMAIL) .setServiceAccountScopes(Collections.singleton("https://www.google.com/m8/feeds/")) .setServiceAccountPrivateKeyFromP12File(p12FileResource.getFile()) .setServiceAccountUser(accountUser).build(); if (!credential.refreshToken()) { throw new RuntimeException("Failed OAuth to refresh the token"); }/*from w w w.j a va2 s .c o m*/ ContactsService service = new ContactsService(APPLICATION_NAME); service.setOAuth2Credentials(credential); service.setHeader("GData-Version", "3.0"); return service; }
From source file:com.fullgear.dataflow.Analyze.java
License:Open Source License
/** * Connects to the Natural Language API using Application Default Credentials. */// w w w . j a va2 s .c o m public static CloudNaturalLanguageAPI getLanguageService() throws IOException, GeneralSecurityException { 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:com.gcloud.poc.DataFlow_File2File.java
License:Apache License
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()); }// www . j ava2 s.c o m HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); storageService = new Storage.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build(); } return storageService; }
From source file:com.github.achatain.catalog.module.MockedAuthenticationModule.java
License:Open Source License
@Provides private GoogleIdTokenVerifier provideGoogleIdTokenVerifier() { try {/*from w w w . j av a 2 s . c o m*/ final HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new GoogleIdTokenVerifier.Builder(transport, jsonFactory).setIssuer(ISSUER).build(); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException("Could not create a GoogleIdTokenVerifier", e); } }
From source file:com.github.drbookings.google.GoogleCalendarSync.java
License:Open Source License
public GoogleCalendarSync init() throws Exception { // initialize the transport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); // initialize the data store factory dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // authorization final Credential credential = authorize(); // set up global Calendar instance client = new com.google.api.services.calendar.Calendar.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build(); return this; }
From source file:com.github.robozonky.integrations.stonky.Util.java
License:Apache License
public static HttpTransport createTransport() { try {//from w w w. ja va 2 s .c o m return GoogleNetHttpTransport.newTrustedTransport(); } catch (final Exception ex) { throw new IllegalStateException(ex); } }
From source file:com.google.android.work.emmnotifications.ServiceAccountConfiguration.java
License:Open Source License
public static Pubsub createPubsubClient(String serviceAccountEmail, String privateKeyFilePath) throws IOException, GeneralSecurityException { HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = new GoogleCredential.Builder().setTransport(transport) .setJsonFactory(JSON_FACTORY).setServiceAccountScopes(PubsubScopes.all()) // Obtain this from the "APIs & auth" -> "Credentials" // section in the Google Developers Console: // https://console.developers.google.com/ // (and put the e-mail address into your system property obviously) .setServiceAccountId(serviceAccountEmail) // Download this file from "APIs & auth" -> "Credentials" // section in the Google Developers Console: // https://console.developers.google.com/ .setServiceAccountPrivateKeyFromP12File(new File(privateKeyFilePath)).build(); // Please use custom HttpRequestInitializer for automatic // retry upon failures. We provide a simple reference // implementation in the "Retry Handling" section. HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Pubsub.Builder(transport, JSON_FACTORY, initializer).setApplicationName("PubSub Example") .build();/*from w w w. j a v a 2 s. c o m*/ }
From source file:com.google.appengine.tck.teamcity.ReportsFeature.java
License:Open Source License
public ReportsFeature(EventDispatcher<BuildServerListener> dispatcher, @NotNull ReportsDescriptor descriptor, @NotNull ReportsConstants constants) { this.editParametersUrl = descriptor.getFeaturePath(); this.constants = constants; this.jsonFactory = JacksonFactory.getDefaultInstance(); try {/*from www . j a v a 2 s. c o m*/ this.httpTransport = GoogleNetHttpTransport.newTrustedTransport(); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException(e); } if (dispatcher != null) { dispatcher.addListener(new BuildServerAdapter() { @Override public void buildFinished(SRunningBuild build) { handleBuildFinished(build); } }); } }