我想为客户创建物联网中心。我使用rest API创建了IOT Hub,但无法找到在其中创建设备的方法。我正在使用Rest API来创建IOT中心。它被创建但是当我尝试创建设备时,我有未经授权的例外。我正在使用this api。
答案 0 :(得分:1)
您需要使用IoT Hub SAS令牌来创建如下设备:
PUT /devices/device2?api-version=2016-11-14 HTTP/1.1
Host: [IOT-HUB-NAME].azure-devices.net
Authorization: SharedAccessSignature sr=[IOT-HUB-NAME].azure-devices.net&sig=[SIG]&se=1557553675&skn=iothubowner
Content-Type: application/json
{deviceId:"device2"}
您可以从device explorer获取IoT Hub SAS令牌。
答案 1 :(得分:1)
使用 SDK RegistryManager
中的 Microsoft.Azure.Devices
和 Azure 门户中共享访问策略选项卡中的连接字符串 registry write
:
var registryManager = RegistryManager.CreateFromConnectionString(connectionString);
var device = new Microsoft.Azure.Devices.Device(name);
var deviceWithKeys = await registryManager.AddDeviceAsync(device);