56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
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)
|
|
|