Skip to content

Instantly share code, notes, and snippets.

@snobu
Created November 27, 2018 14:11
Show Gist options
  • Save snobu/1ba969f3eb459a5bced1b13b76f8ae10 to your computer and use it in GitHub Desktop.
Save snobu/1ba969f3eb459a5bced1b13b76f8ae10 to your computer and use it in GitHub Desktop.
acquire_cognitive_services_bearer_token.py
import requests
import base64
SUBSCRIPTION_KEY = ''
ISSUE_TOKEN_URL = ''
def get_bearer(SUBSCRIPTION_KEY, ISSUE_TOKEN_URL):
headers = {
'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY,
}
r = requests.post(ISSUE_TOKEN_URL, headers=headers)
try:
base64.urlsafe_b64decode(r.text + '=' * (4 - len(r.text) % 4))
bearer = r.text
except:
raise Exception('Could not acquire Bearer token: ' + r.headers + '\n' + r.text)
return bearer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment