0
votes

I am trying to call a Azure Notification Hub REST API , based on this documentation. As they said , I tried to create a Header of API and it giving me an error "The credentials contained in the authorization header are not in the WRAP format".

My Demo DefaultFullSharedAccessSignature is :

Endpoint=sb://shinetrialhub-ns.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=BaGJbFDQZ+hkbi2MdUj7gU0tOM+aC/k+mez9J/y54Qc=

Here my API: https://shinetrialhub-ns.servicebus.windows.net/shinetrialhub/messages/?api-version=2015-01

by adding valid header (please see the MSDN document)

1

1 Answers

1
votes

You need to generate Shared Access Signature Authentication with Service Bus. I've been using code below to achieve this:

resourceUri: https://shinetrialhub-ns.servicebus.windows.net/shinetrialhub/

keyName: RootManageSharedAccessKey

key: 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
}

Also make sure you have all the right headers, as shown in the documentation.