streamline oauth code and fix refresh_token bug
This commit is contained in:
parent
f56bef27cd
commit
1a78e615da
2 changed files with 13 additions and 19 deletions
|
|
@ -14,7 +14,6 @@ def prepare_headers(session: requests.Session,
|
||||||
auth: Optional[str] = None) -> Dict:
|
auth: Optional[str] = None) -> Dict:
|
||||||
headers = {}
|
headers = {}
|
||||||
if auth:
|
if auth:
|
||||||
try:
|
|
||||||
if os.path.isfile(auth):
|
if os.path.isfile(auth):
|
||||||
with open(auth) as json_file:
|
with open(auth) as json_file:
|
||||||
input_json = json.load(json_file)
|
input_json = json.load(json_file)
|
||||||
|
|
@ -27,11 +26,6 @@ def prepare_headers(session: requests.Session,
|
||||||
else:
|
else:
|
||||||
headers = CaseInsensitiveDict(input_json)
|
headers = CaseInsensitiveDict(input_json)
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(
|
|
||||||
"Failed loading provided credentials. Make sure to provide a string or a file path. "
|
|
||||||
"Reason: " + str(e))
|
|
||||||
|
|
||||||
else: # no authentication
|
else: # no authentication
|
||||||
headers = initialize_headers()
|
headers = initialize_headers()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ class YTMusicOAuth:
|
||||||
def dump_token(token, filepath):
|
def dump_token(token, filepath):
|
||||||
token["expires_at"] = int(time.time()) + int(token["expires_in"])
|
token["expires_at"] = int(time.time()) + int(token["expires_in"])
|
||||||
with open(filepath, encoding="utf8", mode="w") as file:
|
with open(filepath, encoding="utf8", mode="w") as file:
|
||||||
json.dump(token, file)
|
json.dump(token, file, indent=True)
|
||||||
|
|
||||||
def setup(self, filepath: Optional[str] = None) -> Dict:
|
def setup(self, filepath: Optional[str] = None) -> Dict:
|
||||||
code = self.get_code()
|
code = self.get_code()
|
||||||
|
|
@ -63,7 +63,7 @@ class YTMusicOAuth:
|
||||||
def load_headers(self, token: Dict, filepath: Optional[str] = None):
|
def load_headers(self, token: Dict, filepath: Optional[str] = None):
|
||||||
headers = initialize_headers()
|
headers = initialize_headers()
|
||||||
if time.time() > token["expires_at"] - 3600:
|
if time.time() > token["expires_at"] - 3600:
|
||||||
token = self.refresh_token(token["refresh_token"])
|
token.update(self.refresh_token(token["refresh_token"]))
|
||||||
self.dump_token(token, filepath)
|
self.dump_token(token, filepath)
|
||||||
headers["Authorization"] = f"{token['token_type']} {token['access_token']}"
|
headers["Authorization"] = f"{token['token_type']} {token['access_token']}"
|
||||||
headers["Content-Type"] = "application/json"
|
headers["Content-Type"] = "application/json"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue