Здравствуйте, хотите спросить, как добавить устройство в облачный реестр Google с помощью ядра google cloud iot с C#
Я пробовал следующий код, который указан в консоли Google для добавления устройства в реестр, но получаю ошибку Google.Apis.Requests.Request Error. У вызывающего абонента нет разрешения [403]
public static CloudIotService CreateAuthorizedClient()
{
GoogleCredential credential =
GoogleCredential.GetApplicationDefaultAsync().Result;
// Inject the Cloud IoT Core Service scope
if (credential.IsCreateScopedRequired)
{
credential = credential.CreateScoped(new[]
{
CloudIotService.Scope.CloudPlatform // Used for IoT + PubSub + IAM
//CloudIotService.Scope.Cloudiot // Can be used if not accessing Pub/Sub
});
}
return new CloudIotService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
GZipEnabled = false
});
}
public static object CreateRsaDevice(string projectId, string cloudRegion, string registryId, string deviceId, string keyPath)
{
keyPath = @"E:\Downloads\thermostat-289211-bb93742bc12f.json";
projectId= "thermostat - 289211";
cloudRegion = "us-central1";
registryId = "testregistry";
deviceId = "C418B9962BC91";
var cloudIot = CreateAuthorizedClient();
var parent = $"projects/{projectId}/locations/{cloudRegion}/registries/{registryId}";
try
{
//var credentials = GoogleCredential.FromFile(keyPath);
//String keyText = File.ReadAllText(keyPath);
Device body = new Device()
{
Id = deviceId
};
body.Credentials = new List<DeviceCredential>();
body.Credentials.Add(new DeviceCredential()
{
PublicKey = new PublicKeyCredential()
{
Key = "here i am entering my public key",
Format = "RSA_X509_PEM"
},
});
var device = cloudIot.Projects.Locations.Registries.Devices.Create(body, parent).Execute();
Console.WriteLine("Device created: ");
Console.WriteLine($"{device.Id}");
Console.WriteLine($"\tBlocked: {device.Blocked == true}");
Console.WriteLine($"\tConfig version: {device.Config.Version}");
Console.WriteLine($"\tName: {device.Name}");
Console.WriteLine($"\tState:{device.State}");
}
catch (Google.GoogleApiException e)
{
Console.WriteLine(e.Message);
if (e.Error != null) return e.Error.Code;
return -1;
}
return 0;
}
прикрепление изображения ролей на странице «Я администратор», выделенной желтым цветом, — это текущая учетная запись, которую я использую