简体   繁体   English

使用Rest API在Azure IoT中心中创建共享访问策略

[英]Shared Access Policy Creation in Azure IoT Hub using Rest API

I am new to azure iot. 我是天蓝色的物联网新手。 and i am trying to create shared access policies in azure iot hub using its rest api. 我正在尝试使用其其余API在azure物联网中心中创建共享访问策略。

 https://management.azure.com/subscriptions/{subscription-Id}/resourceGroups/{group-name}/providers/Microsoft.Devices/IotHubs/{hub-name}?api-version=2016-02-03");

and my java code is 我的java代码是

     String policyold = "{\"tags\" : {}, \"location\": \"East Asia\",\"properties\" : \"authorizationPolicies\" : [{\"name\" : \"policy-namw\", \"primaryKey\" : \"{mykey}\" ,\"secondaryKey\" : \"secondary-key\" ,\"permissions\" :[\"ServiceConnect\" ,\"RegistryRead\" ,\"RegistryWrite\" ,\"DeviceConnect\"]}],\"eventHubEndpoints\" : { \"events\" : {\"messageRetentionInDays\":\"2\"}}}";


    StringEntity input1 = new StringEntity(policyold);
    input1.setContentType("application/json");
    input1.setContentEncoding("UTF8");
    put.setEntity(input1);

    put.setHeader("Authorization", token);
    HttpResponse r2 = httpclient2.execute(put);
    System.out.println(r2.getStatusLine());
    String content2 = EntityUtils.toString(r2.getEntity());
    org.json.JSONObject recvObj2 = new org.json.JSONObject(content2);

but i am facing the followiing error. 但我面临着后续的错误。

 HTTP/1.1 400 Bad Request  {"error":{"code":"InvalidRequestContent","message":"The request content was invalid and could not be deserialized: 'Error converting value \"authorizationPolicies\" to type 'System.Collections.Generic.Dictionary`2[System.String,Newtonsoft.Json.Linq.JToken]'. Path 'properties', line 1, position 76.'."}}

Moreover i am using this tutorial. 此外,我正在使用本教程。 https://msdn.microsoft.com/en-us/library/mt589015.aspx https://msdn.microsoft.com/zh-CN/library/mt589015.aspx

Can any one help me in solving this? 谁能帮我解决这个问题?

According to the offical document Common error codes for Azure IoTHub, the error code 400 means "The body of the request is not valid; for example, it cannot be parsed, or the object cannot be validated.". 根据Azure IoTHub的官方文档“ Common error codes ”,错误代码400表示“请求的主体无效;例如,无法对其进行解析,或者无法验证对象”。

I checked the policyold string value in your code, then I found the json string missed the required elements Sku name & Units . 我检查了代码中的policyold字符串值,然后发现json字符串缺少必需的元素Sku nameUnits Please carefully see the table of the elements below the end of the Json request content. 请仔细查看Json请求内容结尾下方的元素表。

An Azure IoTHub can own multiple shared access policies. Azure IoTHub可以拥有多个共享访问策略。

So if creating shared access policy while creating new IoTHub, please use the REST API Create a new IoT Hub , else use the REST API Update metadata on an existing IoT Hub to add a new one for an existing IoTHub. 因此,如果在创建新的IoTHub时创建共享访问策略,请使用REST API Create a new IoT Hub ,否则Update metadata on an existing IoT Hub使用REST API Update metadata on an existing IoT Hub为现有IoTHub添加新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM