From 6521b5ccb407947f10a5d44bdbafc0afd9613b72 Mon Sep 17 00:00:00 2001 From: tonydero Date: Wed, 25 Dec 2024 19:59:39 -0700 Subject: [PATCH] separate token gen and api pulls --- .gitignore | 1 + oauth-curlsp.py | 35 ++--------------------------------- oauth-token.py | 3 ++- 3 files changed, 5 insertions(+), 34 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f073cd --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.oauthtoken diff --git a/oauth-curlsp.py b/oauth-curlsp.py index 37df14b..e9b9fe5 100644 --- a/oauth-curlsp.py +++ b/oauth-curlsp.py @@ -3,7 +3,6 @@ from oauthlib.oauth2 import WebApplicationClient import webbrowser import urllib3 import re -import requests import subprocess import ast @@ -15,40 +14,10 @@ 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 +token_file = open(".oauthtoken", "r") +oauth_token = token_file.readline() -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 diff --git a/oauth-token.py b/oauth-token.py index ccc42bd..dcd5d69 100644 --- a/oauth-token.py +++ b/oauth-token.py @@ -43,4 +43,5 @@ data = {'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'redirect_uri': 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) +token_file = open(".oauthtoken", "w") +token_file.write(oauth_token)