Hi @waiiki Greetings! Welcome to Microsoft Q&A forum. Thank you for posting this question here.
If you inspect the device template IDs returned though the list of devices API https://{subdomain}.azureiotcentral.com/api/devices?api-version=2022-07-31, you would notice that Template ID returned from the API is different from what you see on the IoT Central portal. Perhaps it is Hashed for security purposes. Here is an example for one of the devices returned though the API.
Inspecting the Device Template on the Azure Portal reveals that this is not ID you have specified for the template.
A work around for this is to create a test device on the IoT Central portal by assigning the Template desired and fetch the template ID using the device list API.
Kindly note that the API would need a Bearer token for authentication. Once you get the correct Template ID, you can use the following code to create a Device.
using System;
using System.Diagnostics;
using Newtonsoft.Json;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Azure.Devices.Provisioning.Client;
using Microsoft.Azure.Devices.Provisioning.Client.Transport;
namespace BulkProvision // Note: actual namespace depends on the project name.
{
internal class Program
{
static string subdomain = "<subdomain>";
static string baseDomain = "azureiotcentral.com";
static string displayName = "<DeviceName";
static string apiVersion = "2022-07-31";
static string DeviceId = "TestDPS1";
static string modelId = "dtmi:dfwvpjdt:uwszlqrlil";
static async Task Main(string[] args)
{
var token = GetAccessToken();
Console.WriteLine(token);
await CreateDevice(token);
await GetDeviceCredentials(token);
}
public static string GetAccessToken()
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\CLI2\\wbin\\az.cmd",
Arguments = $"account get-access-token --resource https://apps.azureiotcentral.com",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output.Split('"')[3];
}
static async Task CreateDevice(string AccessToken)
{
string url = $"https://{subdomain}.{baseDomain}/api/devices/{DeviceId}?api-version={apiVersion}";
string accessToken = AccessToken;
string requestBody = $@"{{""name"": ""{displayName}"",""displayName"": ""{displayName}"",""template"": ""{modelId}"",""enabled"": true,""simulated"": false}}";
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("PUT"), url))
{
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
request.Content = new StringContent(requestBody, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Device updated successfully.");
}
else
{
Console.WriteLine($"Failed to update device. Status code: {response}");
}
}
}
}
}
}
Please note that the above code uses
Azure CLIand references it's installation path using the FileName parameter. Please make sure to install it and refer the corresponding path if you prefer to use this code. Also, modify the code to assign the Subdomain, device name, device ID, model ID per your needs.
Hope this helps. Please let us know if you have any additional questions or need further assistance.
If the response helped, please do click Accept Answer and Yes. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.