From 6979a89ff05a4af00f6a38b38f6ffb9be3d22d38 Mon Sep 17 00:00:00 2001 From: tonydero Date: Tue, 24 Dec 2024 20:05:41 -0700 Subject: [PATCH] requests-based api call not working --- noauth-test.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ oauth-test.py | 28 +++++++++++++++++++------ testing.sh | 9 ++++---- toons-avglvl.sh | 2 +- 4 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 noauth-test.py diff --git a/noauth-test.py b/noauth-test.py new file mode 100644 index 0000000..ed2d65e --- /dev/null +++ b/noauth-test.py @@ -0,0 +1,55 @@ +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) + +oauth_header = {'Authorization': 'Bearer {}'.format(oauth_token)} +profile_data = {'namespace': 'dynamic-us', 'locale': 'en_US'} +profile_url = "https://us.api.blizzard.com/data/wow/token/index" +# | jq -r '[ .wow_accounts[0].characters[].level | select(.) ] | add / length' +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.reason) + diff --git a/oauth-test.py b/oauth-test.py index eedb299..34a906a 100644 --- a/oauth-test.py +++ b/oauth-test.py @@ -4,6 +4,10 @@ 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): @@ -18,15 +22,12 @@ def wait_for_request(server_class=http.server.HTTPServer, def authenticate(): - CLIENT_ID = "a79bae348867427690ca6df9903e4af0" - AUTH_URL = "https://oauth.battle.net/authorize" - client = WebApplicationClient(CLIENT_ID) url = client.prepare_request_uri( AUTH_URL, - redirect_uri = 'http://localhost:5635/login_success', - scope = ['openid'], + redirect_uri = REDIRECT_URI, + scope = ['wow.profile'], state = 'AbCdEfG' ) webbrowser.open(url) @@ -34,4 +35,19 @@ def authenticate(): authenticate() response_code = re.search('code=(.*)&state=', SavingRequestHandler.response_path).group(1) -print("this is the way ->", response_code) \ No newline at end of file +# 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'] + +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" +# | 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) +print(profile_response.url) +print(profile_response) diff --git a/testing.sh b/testing.sh index 6d8eb6d..27d745a 100644 --- a/testing.sh +++ b/testing.sh @@ -1,9 +1,10 @@ CLIENT_ID="a79bae348867427690ca6df9903e4af0" CLIENT_SECRET="VfFc4JRxn4MpA5zRVut1gPIh6E5WrEC5" -CLIENT_ACCESS_TOKEN_RESPONSE="$(curl -u $CLIENT_ID:$CLIENT_SECRET -d grant_type=client_credentials https://oauth.battle.net/token)" -CLIENT_ACCESS_TOKEN=$(echo $CLIENT_ACCESS_TOKEN_RESPONSE | jq -r '.access_token') -OAUTH_ACCESS_TOKEN_RESPONSE="$(curl -X POST https://oauth.battle.net/token -u $CLIENT_ID:$CLIENT_SECRET -d http://localhost -d grant_type=authorization_code -d code=$CLIENT_ACCESS_TOKEN)" -OAUTH_ACCESS_TOKEN=$(echo $OAUTH_ACCESS_TOKEN_RESPONSE | jq -r '.access_token') +# CLIENT_ACCESS_TOKEN_RESPONSE="$(curl -u $CLIENT_ID:$CLIENT_SECRET -d grant_type=client_credentials https://oauth.battle.net/token)" +# CLIENT_ACCESS_TOKEN=$(echo $CLIENT_ACCESS_TOKEN_RESPONSE | jq -r '.access_token') +OAUTH_ACCESS_CODE="USGUSBUZGNQ29JBWKMVFWHIUKIUXUHWW88" +OAUTH_ACCESS_TOKEN_RESPONSE="$(curl -X POST https://oauth.battle.net/token -u $CLIENT_ID:$CLIENT_SECRET -d http://localhost:5635/login_success -d grant_type=authorization_code -d code=$OAUTH_ACCESS_CODE)" +# OAUTH_ACCESS_TOKEN=$(echo $OAUTH_ACCESS_TOKEN_RESPONSE | jq -r '.access_token') echo $OAUTH_ACCESS_TOKEN_RESPONSE # curl -H "Authorization: Bearer $CLIENT_ACCESS_TOKEN" "https://us.api.blizzard.com/data/wow/token/?namespace=dynamic-us" | jq # curl -H "Authorization: Bearer $CLIENT_ACCESS_TOKEN" "https://us.api.blizzard.com/data/wow/guild/proudmoore/stellar/roster?namespace=profile-us" | jq '.members[].character.name' diff --git a/toons-avglvl.sh b/toons-avglvl.sh index 024eb97..8122acb 100644 --- a/toons-avglvl.sh +++ b/toons-avglvl.sh @@ -1 +1 @@ -curl -sH "Authorization: Bearer USCdfWcbxv1hGYQDPWSXbvr84SHme9eJ5T" "https://us.api.blizzard.com/profile/user/wow?namespace=profile-us&locale=en_US" | jq -r '[ .wow_accounts[0].characters[].level | select(.) ] | add / length' +curl -sH "Authorization: Bearer USu4c0C0GINnehnDBOmip4ShEDIXUQ6BIO" "https://us.api.blizzard.com/profile/user/wow?namespace=profile-us&locale=en_US" | jq -r '[ .wow_accounts[0].characters[].level | select(.) ] | add / length'