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.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Print all of the devices in this registry to standard out. */ public static void listDevices(String projectId, String cloudRegion, 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 registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName; List<Device> devices = service.projects().locations().registries().devices().list(registryPath).execute() .getDevices();/* w w w. j a va 2 s. c o m*/ if (devices != null) { System.out.println("Found " + devices.size() + " devices"); for (Device d : devices) { System.out.println("Id: " + d.getId()); if (d.getConfig() != null) { // Note that this will show the device config in Base64 encoded format. System.out.println("Config: " + d.getConfig().toPrettyString()); } System.out.println(); } } else { System.out.println("Registry has no devices."); } }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Create a device that is authenticated using ES256. */ public static void createDeviceWithEs256(String deviceId, String publicKeyFilePath, String projectId, String cloudRegion, 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(); final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;//from w w w. j a v a 2 s. c om PublicKeyCredential publicKeyCredential = new PublicKeyCredential(); final String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8); publicKeyCredential.setKey(key); publicKeyCredential.setFormat("ES256_PEM"); DeviceCredential devCredential = new DeviceCredential(); devCredential.setPublicKey(publicKeyCredential); System.out.println("Creating device with id: " + deviceId); Device device = new Device(); device.setId(deviceId); device.setCredentials(Arrays.asList(devCredential)); Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device) .execute(); System.out.println("Created device: " + createdDevice.toPrettyString()); }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Create a device that is authenticated using RS256. */ public static void createDeviceWithRs256(String deviceId, String certificateFilePath, String projectId, String cloudRegion, 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(); final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;/* w w w . j a v a2s .c o m*/ PublicKeyCredential publicKeyCredential = new PublicKeyCredential(); String key = Files.toString(new File(certificateFilePath), Charsets.UTF_8); publicKeyCredential.setKey(key); publicKeyCredential.setFormat("RSA_X509_PEM"); DeviceCredential devCredential = new DeviceCredential(); devCredential.setPublicKey(publicKeyCredential); System.out.println("Creating device with id: " + deviceId); Device device = new Device(); device.setId(deviceId); device.setCredentials(Arrays.asList(devCredential)); Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device) .execute(); System.out.println("Created device: " + createdDevice.toPrettyString()); }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** * Create a device that has no credentials. * * <p>This is a valid way to construct a device, however until it is patched with a credential the * device will not be able to connect to Cloud IoT. *///from w w w .j a v a 2 s. co m public static void createDeviceWithNoAuth(String deviceId, String projectId, String cloudRegion, 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(); final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName; System.out.println("Creating device with id: " + deviceId); Device device = new Device(); device.setId(deviceId); device.setCredentials(new ArrayList<DeviceCredential>()); Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device) .execute(); System.out.println("Created device: " + createdDevice.toPrettyString()); }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Delete the given device from the registry. */ public static void deleteDevice(String deviceId, String projectId, String cloudRegion, 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(); final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;// www . j a va 2 s .c o m final String devicePath = registryPath + "/devices/" + deviceId; System.out.println("Deleting device " + devicePath); service.projects().locations().registries().devices().delete(devicePath).execute(); }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Retrieves device metadata from a registry. **/ public static Device getDevice(String deviceId, String projectId, String cloudRegion, 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 registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName; String devicePath = registryPath + "/devices/" + deviceId; System.out.println("Retrieving device " + devicePath); return service.projects().locations().registries().devices().get(devicePath).execute(); }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Retrieves device metadata from a registry. **/ public static List<DeviceState> getDeviceStates(String deviceId, String projectId, String cloudRegion, 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 registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName; String devicePath = registryPath + "/devices/" + deviceId; System.out.println("Retrieving device states " + devicePath); ListDeviceStatesResponse resp = service.projects().locations().registries().devices().states() .list(devicePath).execute(); return resp.getDeviceStates(); }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Retrieves registry metadata from a project. **/ public static DeviceRegistry getRegistry(String projectId, String cloudRegion, 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(); final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;//from www. j a va2 s. c o m return service.projects().locations().registries().get(registryPath).execute(); }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** List all of the configs for the given device. */ public static void listDeviceConfigs(String deviceId, String projectId, String cloudRegion, 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(); final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;// w w w.j av a 2s. c o m final String devicePath = registryPath + "/devices/" + deviceId; System.out.println("Listing device configs for " + devicePath); List<DeviceConfig> deviceConfigs = service.projects().locations().registries().devices().configVersions() .list(devicePath).execute().getDeviceConfigs(); for (DeviceConfig config : deviceConfigs) { System.out.println("Config version: " + config.getVersion()); System.out.println("Contents: " + config.getBinaryData()); System.out.println(); } }
From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java
License:Apache License
/** Lists all of the registries associated with the given project. */ public static void listRegistries(String projectId, String cloudRegion) 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; List<DeviceRegistry> registries = service.projects().locations().registries().list(projectPath).execute() .getDeviceRegistries();/*www.j a v a 2 s . c o m*/ if (registries != null) { System.out.println("Found " + registries.size() + " registries"); for (DeviceRegistry r : registries) { System.out.println("Id: " + r.getId()); System.out.println("Name: " + r.getName()); if (r.getMqttConfig() != null) { System.out.println("Config: " + r.getMqttConfig().toPrettyString()); } System.out.println(); } } else { System.out.println("Project has no registries."); } }