oauth: fix string returned by setup_oauth to include expires_in
This commit is contained in:
parent
2825e734c5
commit
8aee43837b
2 changed files with 20 additions and 13 deletions
|
|
@ -37,9 +37,6 @@ class TestYTMusic(unittest.TestCase):
|
|||
assert isinstance(yt, YTMusic)
|
||||
cls.yt = YTMusic()
|
||||
cls.yt_oauth = YTMusic(headers_oauth)
|
||||
with open(headers_oauth, mode="r", encoding="utf8") as headers:
|
||||
string_headers = headers.read()
|
||||
cls.yt_oauth = YTMusic(string_headers)
|
||||
cls.yt_auth = YTMusic(headers_browser)
|
||||
cls.yt_brand = YTMusic(config["auth"]["headers"], config["auth"]["brand_account"])
|
||||
cls.yt_empty = YTMusic(config["auth"]["headers_empty"],
|
||||
|
|
@ -54,7 +51,6 @@ class TestYTMusic(unittest.TestCase):
|
|||
headers = main()
|
||||
self.assertGreaterEqual(len(headers), 2)
|
||||
|
||||
# @unittest.skip("Cannot test oauth flow non-interactively")
|
||||
@mock.patch("requests.Response.json")
|
||||
@mock.patch("requests.Session.post")
|
||||
@mock.patch("sys.argv", ["ytmusicapi", "oauth", "--file", headers_oauth])
|
||||
|
|
@ -68,6 +64,11 @@ class TestYTMusic(unittest.TestCase):
|
|||
main()
|
||||
self.assertTrue(Path(headers_oauth).exists())
|
||||
|
||||
json_mock.side_effect = None
|
||||
with open(headers_oauth, mode="r", encoding="utf8") as headers:
|
||||
string_headers = headers.read()
|
||||
self.yt_oauth = YTMusic(string_headers)
|
||||
|
||||
###############
|
||||
# BROWSING
|
||||
###############
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
import json
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Dict, Optional
|
||||
|
||||
import requests
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from ytmusicapi.constants import (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET,
|
||||
OAUTH_CODE_URL, OAUTH_SCOPE, OAUTH_TOKEN_URL,
|
||||
OAUTH_USER_AGENT)
|
||||
from ytmusicapi.constants import (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_CODE_URL,
|
||||
OAUTH_SCOPE, OAUTH_TOKEN_URL, OAUTH_USER_AGENT)
|
||||
from ytmusicapi.helpers import initialize_headers
|
||||
|
||||
|
||||
|
|
@ -40,8 +40,14 @@ class YTMusicOAuth:
|
|||
response_json = code_response.json()
|
||||
return response_json
|
||||
|
||||
@staticmethod
|
||||
def _parse_token(response) -> Dict:
|
||||
token = response.json()
|
||||
token["expires_at"] = int(time.time()) + int(token["expires_in"])
|
||||
return token
|
||||
|
||||
def get_token_from_code(self, device_code: str) -> Dict:
|
||||
token_response = self._send_request(
|
||||
response = self._send_request(
|
||||
OAUTH_TOKEN_URL,
|
||||
data={
|
||||
"client_secret": OAUTH_CLIENT_SECRET,
|
||||
|
|
@ -49,7 +55,7 @@ class YTMusicOAuth:
|
|||
"code": device_code,
|
||||
},
|
||||
)
|
||||
return token_response.json()
|
||||
return self._parse_token(response)
|
||||
|
||||
def refresh_token(self, refresh_token: str) -> Dict:
|
||||
response = self._send_request(
|
||||
|
|
@ -60,11 +66,12 @@ class YTMusicOAuth:
|
|||
"refresh_token": refresh_token,
|
||||
},
|
||||
)
|
||||
return response.json()
|
||||
return self._parse_token(response)
|
||||
|
||||
@staticmethod
|
||||
def dump_token(token, filepath):
|
||||
token["expires_at"] = int(time.time()) + int(token["expires_in"])
|
||||
if not Path(filepath).is_file():
|
||||
return
|
||||
with open(filepath, encoding="utf8", mode="w") as file:
|
||||
json.dump(token, file, indent=True)
|
||||
|
||||
|
|
@ -73,8 +80,7 @@ class YTMusicOAuth:
|
|||
url = f"{code['verification_url']}?user_code={code['user_code']}"
|
||||
input(f"Go to {url}, finish the login flow and press Enter when done, Ctrl-C to abort")
|
||||
token = self.get_token_from_code(code["device_code"])
|
||||
if filepath:
|
||||
self.dump_token(token, filepath)
|
||||
self.dump_token(token, filepath)
|
||||
|
||||
return token
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue