List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleCredential getApplicationDefault
@Beta public static GoogleCredential getApplicationDefault() throws IOException
From source file:com.example.appengine.firetactoe.FirebaseChannel.java
License:Open Source License
/** * Construct the singleton, with derived auth information. The Firebase database url is derived * from the snippet that we provide to the client code, to guarantee that the client and the * server are communicating with the same Firebase database. The auth credentials we'll use to * communicate with Firebase is derived from App Engine's default credentials, and given * Firebase's OAuth scopes.//from w w w .j ava2s . c om */ private FirebaseChannel() { try { // This variables exist primarily so it can be stubbed out in unit tests. if (null == firebaseConfigStream) { firebaseConfigStream = new FileInputStream(FIREBASE_SNIPPET_PATH); } String firebaseSnippet = CharStreams .toString(new InputStreamReader(firebaseConfigStream, StandardCharsets.UTF_8)); firebaseDbUrl = parseFirebaseUrl(firebaseSnippet); credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); httpTransport = UrlFetchTransport.getDefaultInstance(); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.example.appengine.firetactoe.FirebaseChannel.java
License:Open Source License
public HttpResponse firebasePut(String path, Object object) throws IOException { // Make requests auth'ed using Application Default Credentials Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); String json = new Gson().toJson(object); GenericUrl url = new GenericUrl(path); return requestFactory.buildPutRequest(url, new ByteArrayContent("application/json", json.getBytes())) .execute();/*from www.j ava2 s. c o m*/ }
From source file:com.example.appengine.firetactoe.FirebaseChannel.java
License:Open Source License
public HttpResponse firebasePatch(String path, Object object) throws IOException { // Make requests auth'ed using Application Default Credentials Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); String json = new Gson().toJson(object); GenericUrl url = new GenericUrl(path); return requestFactory.buildPatchRequest(url, new ByteArrayContent("application/json", json.getBytes())) .execute();/*from ww w . j a v a 2s. c o m*/ }
From source file:com.example.appengine.firetactoe.FirebaseChannel.java
License:Open Source License
public HttpResponse firebasePost(String path, Object object) throws IOException { // Make requests auth'ed using Application Default Credentials Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); String json = new Gson().toJson(object); GenericUrl url = new GenericUrl(path); return requestFactory.buildPostRequest(url, new ByteArrayContent("application/json", json.getBytes())) .execute();/*from w w w . j av a 2 s . co m*/ }
From source file:com.example.appengine.firetactoe.FirebaseChannel.java
License:Open Source License
public HttpResponse firebaseGet(String path) throws IOException { // Make requests auth'ed using Application Default Credentials Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); GenericUrl url = new GenericUrl(path); return requestFactory.buildGetRequest(url).execute(); }
From source file:com.example.appengine.firetactoe.FirebaseChannel.java
License:Open Source License
public HttpResponse firebaseDelete(String path) throws IOException { // Make requests auth'ed using Application Default Credentials Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); GenericUrl url = new GenericUrl(path); return requestFactory.buildDeleteRequest(url).execute(); }
From source file:com.example.bigquery.LabelsSample.java
License:Apache License
/** * Add or modify a label on a dataset./*from www. j a va 2s . co m*/ * * <p>See <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">the BigQuery * documentation</a>. */ public static void labelDataset(String projectId, String datasetId, String labelKey, String labelValue) throws IOException { // Authenticate requests using Google Application Default credentials. GoogleCredential credential = GoogleCredential.getApplicationDefault(); credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery")); // Get a new access token. // Note that access tokens have an expiration. You can reuse a token rather than requesting a // new one if it is not yet expired. credential.refreshToken(); String accessToken = credential.getAccessToken(); // Set the content of the request. Dataset dataset = new Dataset(); dataset.addLabel(labelKey, labelValue); HttpContent content = new JsonHttpContent(JSON_FACTORY, dataset); // Send the request to the BigQuery API. String urlFormat = "https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s" + "?fields=labels&access_token=%s"; GenericUrl url = new GenericUrl(String.format(urlFormat, projectId, datasetId, accessToken)); HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(); HttpRequest request = requestFactory.buildPostRequest(url, content); request.setParser(JSON_FACTORY.createJsonObjectParser()); // Workaround for transports which do not support PATCH requests. // See: http://stackoverflow.com/a/32503192/101923 request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH")); HttpResponse response = request.execute(); // Check for errors. if (response.getStatusCode() != 200) { throw new RuntimeException(response.getStatusMessage()); } Dataset responseDataset = response.parseAs(Dataset.class); System.out.printf("Updated label \"%s\" with value \"%s\"\n", labelKey, responseDataset.getLabels().get(labelKey)); }
From source file:com.example.bigquery.LabelsSample.java
License:Apache License
/** * Add or modify a label on a table./* ww w. j av a2s . c o m*/ * * <p>See <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">the BigQuery * documentation</a>. */ public static void labelTable(String projectId, String datasetId, String tableId, String labelKey, String labelValue) throws IOException { // Authenticate requests using Google Application Default credentials. GoogleCredential credential = GoogleCredential.getApplicationDefault(); credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery")); // Get a new access token. // Note that access tokens have an expiration. You can reuse a token rather than requesting a // new one if it is not yet expired. credential.refreshToken(); String accessToken = credential.getAccessToken(); // Set the content of the request. Table table = new Table(); table.addLabel(labelKey, labelValue); HttpContent content = new JsonHttpContent(JSON_FACTORY, table); // Send the request to the BigQuery API. String urlFormat = "https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s/tables/%s" + "?fields=labels&access_token=%s"; GenericUrl url = new GenericUrl(String.format(urlFormat, projectId, datasetId, tableId, accessToken)); HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(); HttpRequest request = requestFactory.buildPostRequest(url, content); request.setParser(JSON_FACTORY.createJsonObjectParser()); // Workaround for transports which do not support PATCH requests. // See: http://stackoverflow.com/a/32503192/101923 request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH")); HttpResponse response = request.execute(); // Check for errors. if (response.getStatusCode() != 200) { throw new RuntimeException(response.getStatusMessage()); } Table responseTable = response.parseAs(Table.class); System.out.printf("Updated label \"%s\" with value \"%s\"\n", labelKey, responseTable.getLabels().get(labelKey)); }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Create a registry for Cloud IoT. */ public static void createRegistry(String cloudRegion, String projectId, String registryName, String pubsubTopicPath) throws GeneralSecurityException, IOException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build(); final String projectPath = "projects/" + projectId + "/locations/" + cloudRegion; final String fullPubsubPath = "projects/" + projectId + "/topics/" + pubsubTopicPath; DeviceRegistry registry = new DeviceRegistry(); EventNotificationConfig notificationConfig = new EventNotificationConfig(); notificationConfig.setPubsubTopicName(fullPubsubPath); List<EventNotificationConfig> notificationConfigs = new ArrayList<EventNotificationConfig>(); notificationConfigs.add(notificationConfig); registry.setEventNotificationConfigs(notificationConfigs); registry.setId(registryName);//w ww .j av a2 s . c om DeviceRegistry reg = service.projects().locations().registries().create(projectPath, registry).execute(); System.out.println("Created registry: " + reg.getName()); }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Delete this registry from Cloud IoT. */ public static void deleteRegistry(String cloudRegion, String projectId, String registryName) throws GeneralSecurityException, IOException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build(); String projectPath = "projects/" + projectId + "/locations/" + cloudRegion; String registryPath = projectPath + "/registries/" + registryName; System.out.println("Deleting: " + registryPath); service.projects().locations().registries().delete(registryPath).execute(); }