From 7b7f3c2f182589f235d8daebd21fbf987a20b47f Mon Sep 17 00:00:00 2001 From: tonydero Date: Wed, 25 Dec 2024 16:33:19 -0700 Subject: [PATCH] subprocess curl functioning --- example.py | 10 +++---- example.sh | 1 + oauth-curlsp.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ oauth-test.py | 28 ++++++++++---------- oauth-token.py | 46 +++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 20 deletions(-) create mode 100644 example.sh create mode 100644 oauth-curlsp.py create mode 100644 oauth-token.py diff --git a/example.py b/example.py index fc10cb8..2b7f47f 100644 --- a/example.py +++ b/example.py @@ -4,7 +4,7 @@ import webbrowser CLIENTID = "a79bae348867427690ca6df9903e4af0" CLIENTSECRET = "VfFc4JRxn4MpA5zRVut1gPIh6E5WrEC5" -OAUTH_TOKEN = "USCdfWcbxv1hGYQDPWSXbvr84SHme9eJ5T" +OAUTH_TOKEN = "USu4c0C0GINnehnDBOmip4ShEDIXUQ6BIO" REGION = "us" LOCALE = "en_US" @@ -20,12 +20,12 @@ api_client = BlizzardApi(CLIENTID, CLIENTSECRET) # print(response) # Unprotected API endpoint -# categories_index = api_client.wow.game_data.get_achievement_categories_index("us", "en_US") +# categories_index = api_client.wow.game_data.get_achievement_categories_index(REGION, LOCALE) # print(categories_index) # Protected API endpoint -# summary = api_client.wow.profile.get_account_profile_summary(REGION, LOCALE, OAUTH_TOKEN) -# print(summary) +summary = api_client.wow.profile.get_account_profile_summary(REGION, LOCALE, OAUTH_TOKEN) +print(summary) # Wow Classic endpoint # connected_realms_index = api_client.wow.game_data.get_connected_realms_index("us", "en_US", is_classic=True) @@ -38,5 +38,3 @@ api_client = BlizzardApi(CLIENTID, CLIENTSECRET) # character_faction = get_character_profile("area-52", "izyrra")["faction"]["name"] # # print(character_faction) - -webbrowser.register('floorp').open_new_tab("https://bbc.co.uk") diff --git a/example.sh b/example.sh new file mode 100644 index 0000000..1b48c9c --- /dev/null +++ b/example.sh @@ -0,0 +1 @@ +curl -H "Authorization: Bearer USu4c0C0GINnehnDBOmip4ShEDIXUQ6BIO" "https://us.api.blizzard.com/data/wow/achievement-category/index?namespace=static-us&locale=en_US" diff --git a/oauth-curlsp.py b/oauth-curlsp.py new file mode 100644 index 0000000..37df14b --- /dev/null +++ b/oauth-curlsp.py @@ -0,0 +1,69 @@ +import http.server +from oauthlib.oauth2 import WebApplicationClient +import webbrowser +import urllib3 +import re +import requests +import subprocess +import ast + + +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + +CLIENT_ID = "a79bae348867427690ca6df9903e4af0" +CLIENT_SECRET = "VfFc4JRxn4MpA5zRVut1gPIh6E5WrEC5" +AUTH_URL = "https://oauth.battle.net/authorize" +REDIRECT_URI = 'http://localhost:5635/login_success' + +class SavingRequestHandler(http.server.SimpleHTTPRequestHandler): + def do_GET(self): + SavingRequestHandler.response_path = self.path + + +def wait_for_request(server_class=http.server.HTTPServer, + handler_class=SavingRequestHandler): + server_address = ('', 5635) + httpd = server_class(server_address, handler_class) + return httpd.handle_request() + + +def authenticate(): + client = WebApplicationClient(CLIENT_ID) + + url = client.prepare_request_uri( + AUTH_URL, + redirect_uri = REDIRECT_URI, + scope = ['wow.profile'], + state = 'AbCdEfG' + ) + webbrowser.open(url) + wait_for_request() + +authenticate() +response_code = re.search('code=(.*)&state=', SavingRequestHandler.response_path).group(1) +# print("this is the way ->", response_code) + +token_url = "https://oauth.battle.net/token" +data = {'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'redirect_uri': REDIRECT_URI, 'grant_type': 'authorization_code', 'code': response_code} +token_response = requests.post(token_url, data=data, auth=requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET), verify=False) + +oauth_token = token_response.json()['access_token'] + +command = ["curl", "-sH", "Authorization: Bearer {}".format(oauth_token), "https://us.api.blizzard.com/profile/user/wow?namespace=profile-us&locale=en_US"] +# | jq -r '[ .wow_accounts[0].characters[].level | select(.) ] | add / length' +# Run the command +result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + +# Get the output and error message (if any) +output = ast.literal_eval(result.stdout) +error = result.stderr + +characters = output['wow_accounts'][0]['characters'] +# Check if it was successful +if result.returncode == 0: + print("Success:") + for char in characters: + print(char['name'], char['level']) +else: + print("Error:") + print(error) diff --git a/oauth-test.py b/oauth-test.py index 34a906a..cff7a5d 100644 --- a/oauth-test.py +++ b/oauth-test.py @@ -33,21 +33,21 @@ def authenticate(): webbrowser.open(url) wait_for_request() -authenticate() -response_code = re.search('code=(.*)&state=', SavingRequestHandler.response_path).group(1) -# print("this is the way ->", response_code) +# authenticate() +# response_code = re.search('code=(.*)&state=', SavingRequestHandler.response_path).group(1) +# # print("this is the way ->", response_code) +# +# # unnecessary? +# token_url = "https://oauth.battle.net/token" +# data = {'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'redirect_uri': REDIRECT_URI, 'grant_type': 'authorization_code', 'code': response_code} +# token_response = requests.post(token_url, data=data, auth=requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET), verify=False) +# +# oauth_token = token_response.json()['access_token'] -# unnecessary? -token_url = "https://oauth.battle.net/token" -data = {'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'redirect_uri': REDIRECT_URI, 'grant_type': 'authorization_code', 'code': response_code} -token_response = requests.post(token_url, data=data, auth=requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET), verify=False) - -oauth_token = token_response.json()['access_token'] - -oauth_header = {'Authorization': 'Bearer {}'.format(oauth_token)} -profile_data = {'namespace': 'profile-us', 'locale': 'en_US'} -profile_url = "https://us.api.blizzard.com/profile/user/wow" +oauth_header = {'Authorization': 'Bearer USu4c0C0GINnehnDBOmip4ShEDIXUQ6BIO'} +profile_data = {'namespace': 'static-us', 'locale': 'en_US'} +profile_url = "https://us.api.blizzard.com/data/wow/achievement-category/index" # | jq -r '[ .wow_accounts[0].characters[].level | select(.) ] | add / length' -profile_response = requests.post(profile_url, headers=oauth_header, params=profile_data, allow_redirects=True) #, auth=requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET), verify=False) +profile_response = requests.post(profile_url, headers=oauth_header, params=profile_data) #, auth=requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET), verify=False) print(profile_response.url) print(profile_response) diff --git a/oauth-token.py b/oauth-token.py new file mode 100644 index 0000000..ccc42bd --- /dev/null +++ b/oauth-token.py @@ -0,0 +1,46 @@ +import http.server +from oauthlib.oauth2 import WebApplicationClient +import webbrowser +import re +import requests + +CLIENT_ID = "a79bae348867427690ca6df9903e4af0" +CLIENT_SECRET = "VfFc4JRxn4MpA5zRVut1gPIh6E5WrEC5" +AUTH_URL = "https://oauth.battle.net/authorize" +REDIRECT_URI = 'http://localhost:5635/login_success' + +class SavingRequestHandler(http.server.SimpleHTTPRequestHandler): + def do_GET(self): + SavingRequestHandler.response_path = self.path + + +def wait_for_request(server_class=http.server.HTTPServer, + handler_class=SavingRequestHandler): + server_address = ('', 5635) + httpd = server_class(server_address, handler_class) + return httpd.handle_request() + + +def authenticate(): + client = WebApplicationClient(CLIENT_ID) + + url = client.prepare_request_uri( + AUTH_URL, + redirect_uri = REDIRECT_URI, + scope = ['wow.profile'], + state = 'AbCdEfG' + ) + webbrowser.open(url) + wait_for_request() + +authenticate() +response_code = re.search('code=(.*)&state=', SavingRequestHandler.response_path).group(1) +# print("this is the way ->", response_code) + +# unnecessary? +token_url = "https://oauth.battle.net/token" +data = {'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'redirect_uri': REDIRECT_URI, 'grant_type': 'authorization_code', 'code': response_code} +token_response = requests.post(token_url, data=data, auth=requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET), verify=False) + +oauth_token = token_response.json()['access_token'] +print(oauth_token)