Skip to content

Instantly share code, notes, and snippets.

@sedouard
Created November 13, 2014 04:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sedouard/8412cb51629ef9614fd4 to your computer and use it in GitHub Desktop.
Save sedouard/8412cb51629ef9614fd4 to your computer and use it in GitHub Desktop.
Generate SAS Token for eventhub from Python
import urllib
import time
import hmac
import hashlib
import base64
def _sign_string(uri, key, key_name):
'''
100000 = milsecond expiry
'''
expiry = int(time.time() + 10000)
string_to_sign = urllib.quote_plus(uri) + '\n' + str(expiry)
print 'url: ' + uri
print 'url encoded: ' + string_to_sign
key = key.encode('utf-8')
string_to_sign = string_to_sign.encode('utf-8')
print 'url encoded: ' + string_to_sign
signed_hmac_sha256 = hmac.HMAC(key, string_to_sign, hashlib.sha256)
signature = signed_hmac_sha256.digest()
signature = base64.b64encode(signature)
return 'SharedAccessSignature sr=' + urllib.quote_plus(uri) + '&sig=' + urllib.quote(signature) + '&se=' + str(expiry) + '&skn=' + key_name
print _sign_string('https://<Service Bus Namespace>.servicebus.windows.net/<Hub Name>/publishers/<Device Id>/messages', '<Your Shared Access Token>', '<Your Key Name>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment