0

I am new to azure iot. and i am trying to create shared access policies in azure iot hub using its rest api.

 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

     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

Can any one help me in solving this?

juvchan
  • 6,113
  • 2
  • 22
  • 35
Sadaf
  • 247
  • 5
  • 16
  • I suggest you can look into the Azure IoT hub Java SDK for simplified API calls. The link is at https://github.com/Azure/azure-iot-sdks/tree/master/java – juvchan Apr 04 '16 at 15:06
  • yes i tried to use this for creating shared access policies. but couldn't find any code example and documentation for this purpose. thats why i end up with http rest api. can u refer one? thanks – Sadaf Apr 05 '16 at 10:36

1 Answers1

1

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.".

I checked the policyold string value in your code, then I found the json string missed the required elements Sku name & Units. Please carefully see the table of the elements below the end of the Json request content.

An Azure IoTHub can own multiple shared access policies.

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.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Okay @PeterPan. now i changed my policyold string to this. 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\"}},\"sku\":{\"name\":\"S1\",\"capacity\":1}}"; but still facing the same issue. – Sadaf Apr 05 '16 at 07:41
  • i am using Update metadata on and existing IoT hub Rest API. – Sadaf Apr 05 '16 at 07:42
  • @Sadaf I suggest that you can try to use the Azure IoTHub SDK for Java as juvchan said at the comment, and use the Fiddler to catch the HTTP request and response to know the REST API calling, because the Java SDK wrapped the related REST APIs. There is [an IoTHub tutorial for getting started in Java](https://azure.microsoft.com/en-us/documentation/articles/iot-hub-java-java-getstarted/) you can refer to. – Peter Pan Apr 05 '16 at 07:56
  • can u please refer any sample of documentation of creating shared access policies using java api. i tried alot but couldn't find any.Thanks – Sadaf Apr 05 '16 at 10:38
  • @Sadaf Please refer to https://github.com/Azure/azure-iot-sdks/blob/master/java/device/iothub-java-client/src/main/java/com/microsoft/azure/iothub/auth/Signature.java. Hope it helps. – Peter Pan Apr 28 '16 at 09:06