Microsoft Azure CreateQueue using Simple REST Client

落爺英雄遲暮 提交于 2019-12-13 07:44:29

问题


I'm trying to create a queue on Azure cloud. I've an Azure account, namespace and using Service Bus. Due to some restrictions I need to do it using RAW GET/PUT requests so I'm using Simple REST Client.

These are the values mentioned in REST client fields:


URL

https://mynamespace-ns.servicebus.windows.net/

Method

PUT

Headers

PUT /testqueue?timeout=30 HTTP/1.1

x-ms-date: Fri, 25 Sep 2015 03:16:12 GMT

x-ms-version: 2009-09-19

Authorization: SharedKey mynamespace-ns:oucfev8CXZPMsli4t7iZJ+nlC0fUwasyPH5OdSqi9po=

Host: mynamespace-ns.servicebus.windows.net

Content-Length: 0


This is how I'm generating the Authorization key:

HmacSha256 encoding the string "PUT\n\n\n\n0\n\n\n\n\n\n\n\nx-ms-date:Fri, 25 Sep 2015 03:16:12 GMT\nx-ms-version:2009-09-19\n/mynamespace-ns" with secret key is the SharedAccessKey copied from Connection Information page on Azure Portal. After that Base64Encode the resulted string.

Every-time I send the request I got the following response:

401MalformedToken: Invalid authorization header: The request is missing WRAP authorization credentials. TrackingId:8d52cae0-0dba-470d-8db2-3e76d4fd4d0b_G27,TimeStamp:9/25/2015 9:45:17 AM

Can anyone please tell what I'm missing or am I doing something wrong?


回答1:


The request has to have an access token attached in the request headers. When using Azure service bus, you need to get a token from the azure access control service. Found this page...

Azure Access Token Patterns

You do not need to use SDK to do all this as I am doing the same from an Android program.

EDIT...

You'll have to adjust this to whatever language you are using.

First get the token...

URL acsUrl = new URL("https://yournamespace-sb.accesscontrol.windows.net/WRAPv0.9/");
URL realm = new URL("http://yournamespace.servicebus.windows.net");
httpConn = (HttpURLConnection) acsUrl.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setUseCaches(false);
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

String body = "wrap_name=" + URLEncoder.encode(AdminConstants.ISSUER, "UTF-8") + 
"&wrap_password=" + URLEncoder.encode(AdminConstants.ISSUER_SECRET, "UTF-8") +
"&wrap_scope=" + URLEncoder.encode(realm,  "UTF-8");
byte[] postBytes = body.getBytes();
httpConn.setRequestProperty("Content-Length", Integer.toString(postBytes.length));
httpConn.setRequestProperty("Expect", "100-continue");
httpConn.setRequestProperty("Accept", "*/*");

/* Fire the request here */

String[] responseProperties = response.toString().split("&");
String[] tokenProperty = responseProperties[0].split("=");
String token = URLDecoder.decode(tokenProperty[1], "UTF-8");

Your realm will be different since I am accessing service bus and you are creating queues.

Finally when you make your call to create the queue you will have to include the token in your post request like this...

httpConn.setRequestProperty("Authorization", "WRAP access_token=\"" + getAcsToken() + "\"");


来源:https://stackoverflow.com/questions/32779585/microsoft-azure-createqueue-using-simple-rest-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!