Created
September 27, 2019 07:09
-
-
Save ChenTanyi/54b3d3250973c736b19a4f36956c6036 to your computer and use it in GitHub Desktop.
Get Azure Auth
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
#!/usr/bin/env python3 | |
import os | |
import sys | |
import json | |
import logging | |
import requests | |
def request_token(): | |
auth_file = os.environ.get('AZURE_AUTH_LOCATION') | |
client_id = None | |
client_secret = None | |
tenant_id = None | |
if not auth_file or not os.path.exists(auth_file): | |
client_id = os.environ.get("AZURE_CLIENT_ID") | |
client_secret = os.environ.get("AZURE_CLIENT_SECRET") | |
tenant_id = os.environ.get("AZURE_TENANT_ID") | |
if not client_id or not client_secret or not tenant_id: | |
logging.error( | |
'Cannot get AZURE_AUTH_LOCATION or AZURE_CLIENT_ID and AZURE_CLIENT_SECRET and AZURE_TENANT_ID' | |
) | |
sys.exit(1) | |
else: | |
azure_auth = json.load(open(auth_file)) | |
client_id = azure_auth['clientId'] | |
client_secret = azure_auth['clientSecret'] | |
tenant_id = azure_auth['tenantId'] | |
r = requests.post( | |
f'https://login.microsoftonline.com/{tenant_id}/oauth2/token', | |
data={ | |
'resource': 'https://management.core.windows.net/', | |
'client_id': client_id, | |
'client_secret': client_secret, | |
'client_info': 1, | |
'grant_type': 'client_credentials', | |
}) | |
return r.json() | |
def main(): | |
token = request_token()['access_token'] | |
print(token) | |
if 'win' in sys.platform: | |
os.system(f'echo {token} | clip') | |
if __name__ == "__main__": | |
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment