使用简单REST客户端的Microsoft Azure CreateQueue

时间:2015-09-25 09:55:45

标签: rest azure cloud

我尝试在Azure云上创建队列。我是Azure帐户,命名空间和使用Service Bus。由于某些限制,我需要使用RAW GET / PUT请求,因此我使用Simple REST Client

这些是REST客户端字段中提到的值:

网址

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

方式

PUT

接头

PUT / testqueue?timeout = 30 HTTP / 1.1

x-ms-date:2015年9月25日星期五03:16:12 GMT

x-ms-version:2009-09-19

授权:SharedKey mynamespace-ns:oucfev8CXZPMsli4t7iZJ + nlC0fUwasyPH5OdSqi9po =

主持人:mynamespace-ns.servicebus.windows.net

内容长度:0

这就是我生成授权密钥的方式:

HmacSha256编码字符串" PUT \ n \ n \ n \ n0 \ n \ n \ n \ n \ n \ n \ n \ nx-ms-date:星期五,2015年9月25日03:16: 12 GMT \ nx-ms-version:2009-09-19 \ n / mynamespace-ns"使用密钥是从Azure门户上的“连接信息”页面复制的SharedAccessKey。在Base64Encode之后生成了字符串。

每次发送请求时,我都会收到以下回复:

401 MalformedToken:无效的授权标头:请求缺少WRAP授权凭据。 TrackingId:8d52cae0-0dba-470d-8db2-3e76d4fd4d0b_G27,TimeStamp:9/25/2015 9:45:17 AM

任何人都可以告诉我我失踪了什么,或者我做错了什么?

1 个答案:

答案 0 :(得分:1)

请求必须在请求标头中附加访问令牌。使用Azure服务总线时,您需要从azure访问控制服务获取令牌。找到这个页面......

Azure Access Token Patterns

您不需要使用SDK来完成所有这些操作,因为我在Android程序中也这样做。

编辑...

您必须将其调整为您正在使用的任何语言。

首先获得令牌......

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");

你的领域会有所不同,因为我正在访问服务总线而你正在创建队列。

最后当你打电话来创建队列时,你必须在你的帖子请求中包含这样的令牌......

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