add_azure_account_and_set_role_assignment_5.py 763 B

1234567891011121314151617181920
  1. def get_azure_bearer_token(resource_url, azure_directory_id, azure_admin_application_id,
  2. azure_admin_application_secret):
  3. """
  4. Uses OAuth 2.0 to get the bearer token based on the client id and client secret.
  5. """
  6. api_url = "https://login.microsoftonline.com/" + azure_directory_id + "/oauth2/token"
  7. client = {'grant_type': 'client_credentials',
  8. 'client_id': azure_admin_application_id,
  9. 'client_secret': azure_admin_application_secret,
  10. 'resource': resource_url,
  11. }
  12. response = requests.post(api_url, data=client)
  13. if "access_token" in response.json():
  14. return response.json()["access_token"]
  15. print("Could not get Bearer token")
  16. return None