Search in sources :

Example 1 with PublicKeyCredential

use of com.google.api.services.cloudiot.v1.model.PublicKeyCredential in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method patchEs256ForAuth.

// [END iot_list_registries]
// [START iot_patch_es]
/**
 * Patch the device to add an ES256 key for authentication.
 */
public static void patchEs256ForAuth(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 devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
    publicKeyCredential.setKey(key);
    publicKeyCredential.setFormat("ES256_PEM");
    DeviceCredential devCredential = new DeviceCredential();
    devCredential.setPublicKey(publicKeyCredential);
    Device device = new Device();
    device.setCredentials(Arrays.asList(devCredential));
    Device patchedDevice = service.projects().locations().registries().devices().patch(devicePath, device).setUpdateMask("credentials").execute();
    System.out.println("Patched device is " + patchedDevice.toPrettyString());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) File(java.io.File)

Example 2 with PublicKeyCredential

use of com.google.api.services.cloudiot.v1.model.PublicKeyCredential in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createDeviceWithEs256.

// [END iot_list_devices]
// [START iot_create_es_device]
/**
 * 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 = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
    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());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) File(java.io.File)

Example 3 with PublicKeyCredential

use of com.google.api.services.cloudiot.v1.model.PublicKeyCredential in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createDeviceWithRs256.

// [END iot_create_es_device]
// [START iot_create_rsa_device]
/**
 * 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 = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
    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());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) File(java.io.File)

Example 4 with PublicKeyCredential

use of com.google.api.services.cloudiot.v1.model.PublicKeyCredential in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method patchRsa256ForAuth.

// [END iot_patch_es]
// [START iot_patch_rsa]
/**
 * Patch the device to add an RSA256 key for authentication.
 */
public static void patchRsa256ForAuth(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 devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
    publicKeyCredential.setKey(key);
    publicKeyCredential.setFormat("RSA_X509_PEM");
    DeviceCredential devCredential = new DeviceCredential();
    devCredential.setPublicKey(publicKeyCredential);
    Device device = new Device();
    device.setCredentials(Arrays.asList(devCredential));
    Device patchedDevice = service.projects().locations().registries().devices().patch(devicePath, device).setUpdateMask("credentials").execute();
    System.out.println("Patched device is " + patchedDevice.toPrettyString());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) File(java.io.File)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)4 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)4 JsonFactory (com.google.api.client.json.JsonFactory)4 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)4 Device (com.google.api.services.cloudiot.v1.model.Device)4 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)4 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)4 File (java.io.File)4