wow-api-characterinfo/oauth-test.py

54 lines
2.0 KiB
Python
Raw Permalink Normal View History

2024-12-24 14:18:55 -07:00
import http.server
2024-12-24 13:41:43 -07:00
from oauthlib.oauth2 import WebApplicationClient
2024-12-24 14:18:55 -07:00
import webbrowser
import re
import requests
2024-12-24 13:41:43 -07:00
2024-12-24 20:05:41 -07:00
CLIENT_ID = "a79bae348867427690ca6df9903e4af0"
CLIENT_SECRET = "VfFc4JRxn4MpA5zRVut1gPIh6E5WrEC5"
AUTH_URL = "https://oauth.battle.net/authorize"
REDIRECT_URI = 'http://localhost:5635/login_success'
2024-12-24 13:41:43 -07:00
2024-12-24 14:18:55 -07:00
class SavingRequestHandler(http.server.SimpleHTTPRequestHandler):
2024-12-24 13:41:43 -07:00
def do_GET(self):
2024-12-24 14:18:55 -07:00
SavingRequestHandler.response_path = self.path
2024-12-24 09:49:14 -07:00
2024-12-24 14:18:55 -07:00
def wait_for_request(server_class=http.server.HTTPServer,
handler_class=SavingRequestHandler):
2024-12-24 09:49:14 -07:00
server_address = ('', 5635)
httpd = server_class(server_address, handler_class)
return httpd.handle_request()
2024-12-24 13:41:43 -07:00
2024-12-24 09:49:14 -07:00
def authenticate():
2024-12-24 13:41:43 -07:00
client = WebApplicationClient(CLIENT_ID)
url = client.prepare_request_uri(
AUTH_URL,
2024-12-24 20:05:41 -07:00
redirect_uri = REDIRECT_URI,
scope = ['wow.profile'],
2024-12-24 13:41:43 -07:00
state = 'AbCdEfG'
)
2024-12-24 09:49:14 -07:00
webbrowser.open(url)
2024-12-24 13:41:43 -07:00
wait_for_request()
2024-12-24 09:49:14 -07:00
2024-12-25 16:33:19 -07:00
# 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']
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"
2024-12-24 20:05:41 -07:00
# | jq -r '[ .wow_accounts[0].characters[].level | select(.) ] | add / length'
2024-12-25 16:33:19 -07:00
profile_response = requests.post(profile_url, headers=oauth_header, params=profile_data) #, auth=requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET), verify=False)
2024-12-24 20:05:41 -07:00
print(profile_response.url)
print(profile_response)