我正在尝试根据此documentation调用Azure通知中心REST API。正如他们所说,我试图创建一个API标题,它给我一个错误“授权标题中包含的凭据不是WRAP格式”。
我的演示版DefaultFullSharedAccessSignature是:
端点= SB://shinetrialhub-ns.servicebus.windows.net/; SharedAccessKeyName = DefaultFullSharedAccessSignature; SharedAccessKey = BaGJbFDQZ + hkbi2MdUj7gU0tOM + AC / K + mez9J / y54Qc =
我的API:https://shinetrialhub-ns.servicebus.windows.net/shinetrialhub/messages/?api-version=2015-01
添加有效标题(请参阅MSDN文档)
答案 0 :(得分:1)
您需要使用Service Bus生成共享访问签名身份验证。我一直在使用以下代码来实现这一目标:
resourceUri:https://shinetrialhub-ns.servicebus.windows.net/shinetrialhub/
keyName:RootManageSharedAccessKey
键:the value for RootManageSharedAccessKey
private string GetSasToken(string resourceUri, string keyName, string key)
{
var expiry = GetExpiry();
var stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = string.Format(CultureInfo.InvariantCulture,
"SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);
return sasToken;
}
private string GetExpiry()
{
var sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
return Convert.ToString((int) sinceEpoch.TotalSeconds + 102000); //token valid for that many seconds
}
另外,请确保您拥有所有正确的标头,如文档中所示。