我是天蓝色的物联网新手。我正在尝试使用其其余api在azure物联网中心中创建共享访问策略。
https://management.azure.com/subscriptions/{subscription-Id}/resourceGroups/{group-name}/providers/Microsoft.Devices/IotHubs/{hub-name}?api-version=2016-02-03");
我的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);
但我面临着后续的错误。
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.'."}}
此外,我正在使用本教程。https://msdn.microsoft.com/zh-CN/library/mt589015.aspx
谁能帮我解决这个问题?
根据Common error codes
Azure IoTHub的官方文档,错误代码400表示“请求的主体无效;例如,无法对其进行解析,或者无法验证对象”。
我检查了policyold
代码中的字符串值,然后发现json字符串缺少必需的元素Sku name
&Units
。请仔细查看Json请求内容结尾下方的元素表。
Azure IoTHub可以拥有多个共享访问策略。
因此,如果在创建新的IoTHub时创建共享访问策略,请使用REST API Create a new IoT Hub
,否则请使用REST APIUpdate metadata on an existing IoT Hub
为现有的IoTHub添加一个新的API 。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句