Search code examples
c#.netazureazure-iot-hub

Get deployment manifest from Iothub configuration using .net


What I want:

I want to duplicate, change and deploy a configuration in iothub using .net, however I cannot figure out how to get the deployment manifest (deployment_manifest.json) using the API.

I want the deployment manifest from my selected configuration as an object.

What I tried:

private static string GetConfiguration(string iotHubName, string token)
        {
            using var client = new HttpClient();

            client.DefaultRequestHeaders.Add("Authorization", token);

            var restUriGet = $"https://{iotHubName}/api/deploymentManifests/opcpublisher?api-version=2022-10-31-preview";

            using var resultGet = client.GetAsync(restUriGet).Result;
            return resultGet.StatusCode.ToString();
        }

This gives me a bad request error


private static string GetConfiguration(string iotHubName, string token)
    {
        using var client = new HttpClient();

        client.DefaultRequestHeaders.Add("Authorization", token);

        var restUriGet = $"https://{iotHubName}/configurations/opcpublisher?api-version=2020-05-31-preview";

        using var resultGet = client.GetAsync(restUriGet).Result;
        return resultGet.StatusCode.ToString();
    }

This gives me a lot of information but no JSON.

Here is the documentation/tutorial I used: microsoft / tutorial


Solution

  • The bad request error could be due to an incorrect manifestId or token.

     

    Check whether you are passing the correct values of manifestId or token. And also check that you have the correct permissions to access the deployment manifest.

     

    string con = "Connection String";
    string device_Id = "deviceId";
    string str = GenSas_Token(con, device_Id);
    
     
    
    var iotHub = "IotHub";
    var deployment_ManifestId = "ManifestId";
    var token = str;
    
     
    
    var clnt = new HttpClient();
    clnt.DefaultRequestHeaders.Add("Authorization", token);
    var restUriGet = $"https://{iotHub}.azureiotcentral.com/api/deploymentManifests/{deployment_ManifestId}?api-version=2022-10-31-preview";
    
     
    
    var resultGet = await clnt.GetAsync(restUriGet);
    var content = await resultGet.Content.ReadAsStringAsync();
    
     
    
    //To fetch Token
    public static string GenSas_Token(string HubConn, string deviceId)
            {
                var builder = new SharedAccessSignatureBuilder()
                {
                    Key = "Key",
                    Target = $"{HubConn}/devices/{deviceId}",
                    TimeToLive = TimeSpan.FromMinutes(20)
                };
    
     
    
                return builder.ToSignature();
            }
    

     

    enter image description here

     

    Thanks @ Sander van de Velde for the Blog.

     

    For further information, refer to the MSDoc.