Created
January 17, 2019 13:13
-
-
Save locatw/d220af2334a55b3ca520fc2250e5a678 to your computer and use it in GitHub Desktop.
Google Cloud IoT Coreに登録されている端末の一覧を取得するプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Google.Apis.Auth.OAuth2; | |
using Google.Apis.CloudIot.v1; | |
using Google.Apis.Services; | |
using System; | |
using System.Threading.Tasks; | |
namespace Sample | |
{ | |
class Program | |
{ | |
private static readonly string ProjectId = "[PROJECT_ID]"; | |
private static readonly string Region = "[REGION_NAME]"; | |
private static readonly string RegistryId = "[REGISTRY_ID]"; | |
static void Main(string[] args) | |
{ | |
Run().Wait(); | |
} | |
static async Task Run() | |
{ | |
// 環境変数"GOOGLE_APPLICATION_CREDENTIALS"が必要。 | |
// 参照: https://cloud.google.com/docs/authentication/getting-started | |
var credential = GoogleCredential.GetApplicationDefault(); | |
if (credential.IsCreateScopedRequired) | |
{ | |
credential = credential.CreateScoped(new[] | |
{ | |
CloudIotService.Scope.CloudPlatform | |
}); | |
} | |
var cloudIotService = new CloudIotService(new BaseClientService.Initializer | |
{ | |
HttpClientInitializer = credential, | |
GZipEnabled = false | |
}); | |
var parent = $"projects/{ProjectId}/locations/{Region}/registries/{RegistryId}"; | |
var getDeviceListResponse = await cloudIotService.Projects.Locations.Registries.Devices.List(parent).ExecuteAsync(); | |
foreach (var device in getDeviceListResponse.Devices) | |
{ | |
var name = $"{parent}/devices/{device.Id}"; | |
var getDeviceReponse = await cloudIotService.Projects.Locations.Registries.Devices.Get(name).ExecuteAsync(); | |
Console.WriteLine($"ID: {getDeviceReponse.Id}, Name: {getDeviceReponse.Metadata["Name"]}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment