Last active
October 27, 2021 19:33
-
-
Save abenbachir/be027bbfe32503dcda73ca3cd59e2815 to your computer and use it in GitHub Desktop.
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 sys | |
import json | |
import os | |
def old_functin(): | |
import urllib2 | |
subscription_id = "5320918a-f859-451d-86ab-c965dcaf589c" | |
amrurl = "https://management.azure.com/subscriptions/" + subscription_id + "?api-version=2014-04-01" | |
print("amrurl=", amrurl) | |
try: | |
req = urllib2.Request(amrurl, headers={'Content-Type':'application/json'}) | |
res = urllib2.urlopen(req) | |
except Exception as e: | |
print("ex", e) | |
err_res = e.headers["WWW-Authenticate"] | |
print("err_res=", err_res) | |
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 | |
def new_function(): | |
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 | |
subscription_id = "5320918a-f859-451d-86ab-c965dcaf589c" | |
arm_domain = "management.azure.com" | |
arm_url = "https://{0}/subscriptions/{1}?api-version=2014-04-01".format(arm_domain, subscription_id) | |
try: | |
print("arm_url=", arm_url) | |
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 | |
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("ex=", e) | |
err_res = e.headers["WWW-Authenticate"] | |
print("err_res=", err_res) | |
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 | |
new_function() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment