Created
November 27, 2018 14:11
-
-
Save snobu/1ba969f3eb459a5bced1b13b76f8ae10 to your computer and use it in GitHub Desktop.
acquire_cognitive_services_bearer_token.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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