Skip to content

Instantly share code, notes, and snippets.

@abenbachir
Last active September 9, 2021 21:13
Show Gist options
  • Save abenbachir/7a2b7a617a5dded4a4f31be08887231f to your computer and use it in GitHub Desktop.
Save abenbachir/7a2b7a617a5dded4a4f31be08887231f to your computer and use it in GitHub Desktop.
import sys
# if sys.version_info[0] < 3:
# from future import standard_library
# standard_library.install_aliases()
# from builtins import str
import urllib.request, urllib.error, urllib.parse
import json
import os
def test_setup_me():
data = ""
aad_auth_url = ""
arm_url = "https://{0}/subscriptions/{1}?api-version=2014-04-01".format("management.azure.com", "5320918a-f859-451d-86ab-c965dcaf589c")
try:
req = urllib.request.Request(arm_url, headers={'Content-Type':'application/json'})
# urlopen alias in future backport is broken on py2.6, fails on urls with HTTPS - https://github.com/PythonCharmers/python-future/issues/167
# Using this hack of switching between py2 and 3 to avoid this
print("Sending HTTP request to arm_url=", arm_url)
if sys.version_info < (2,7):
from urllib2 import HTTPError, Request, urlopen
urlopen(req)
else:
res = urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
print("HTTPErro=", e)
err_res = e.headers["WWW-Authenticate"]
for line in err_res.split(","):
if "Bearer authorization_uri" in line:
data = line.split("=")
aad_auth_url = data[1][1:-1] # Removing the quotes from the front and back
break
except urllib2.URLError as e:
print("Unable to open amrurl=%s due to error=%s" % (arm_url, e))
except Exception as e:
print("Exception Error=%s", e)
return
print("Success: aad_auth_url=%s data=%s" %(aad_auth_url, data))
print(test_setup_me())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment