Registering a IOT Device in C++
-
The C++ API doesn't seem to provide a similar registration interface for device as is provided by the Java. Before I forge ahead and write on myself I want to verify I am not overlooking anything. The Java API that (I believe) accomplishes registration of a device is:
/** 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());
}[^] Any comments much appreciated. Tom
-
The C++ API doesn't seem to provide a similar registration interface for device as is provided by the Java. Before I forge ahead and write on myself I want to verify I am not overlooking anything. The Java API that (I believe) accomplishes registration of a device is:
/** 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());
}[^] Any comments much appreciated. Tom