From c93bd6013b98d60eaa2d00aab628bfe0aea2e10d Mon Sep 17 00:00:00 2001 From: wxy1343 <1343890272@qq.com> Date: Fri, 13 Aug 2021 04:55:19 +0800 Subject: [PATCH] =?UTF-8?q?[+]=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6refresh=5Ftoken=20[+]?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=88=B7=E6=96=B0refresh=5Ftoken=E4=BB=A5?= =?UTF-8?q?=E9=98=B2=E8=BF=87=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aliyunpan/about.py | 2 +- aliyunpan/api/core.py | 25 ++++++++++++++++++------- aliyunpan/api/req.py | 13 ++++++++++++- aliyunpan/cli/cli.py | 3 ++- aliyunpan/cli/config.py | 10 ++++++++-- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/aliyunpan/about.py b/aliyunpan/about.py index cfadc52..1317948 100644 --- a/aliyunpan/about.py +++ b/aliyunpan/about.py @@ -1 +1 @@ -__version__ = '2.7.7' +__version__ = '2.7.8' diff --git a/aliyunpan/api/core.py b/aliyunpan/api/core.py index 807f0a6..5ac128d 100644 --- a/aliyunpan/api/core.py +++ b/aliyunpan/api/core.py @@ -31,6 +31,7 @@ def __init__(self, refresh_token: str = None, album: bool = False): self._username = None self._password = None self._refresh_token = refresh_token + self._refresh_token_expires = None self._access_token_gen_ = self._access_token_gen() self._drive_id_gen_ = self._drive_id_gen() self._chunk_size = 524288 @@ -513,21 +514,31 @@ def get_upload_url(self, path: str, upload_id: str, file_id: str, chunk_size: in i['upload_url'] = '' return part_info_list - def get_access_token(self) -> str: - """ - 获取access_token - :return: - """ + def token_refresh(self): # url = 'https://websv.aliyundrive.com/token/refresh' # json = {"refresh_token": self.refresh_token} url = 'https://auth.aliyundrive.com/v2/account/token' json = {"refresh_token": self.refresh_token, 'grant_type': 'refresh_token'} - logger.info(f'Get ACCESS_TOKEN.') + logger.info(f'Token has been refreshed.') r = self._req.post(url, json=json, access_token=False) try: - access_token = r.json()['access_token'] + self._refresh_token = r.json()['refresh_token'] + self._refresh_token_expires = time.time() + r.json()['expires_in'] except KeyError: raise InvalidRefreshToken + return r.json() + + @property + def refresh_token_expires_sec(self): + return self._refresh_token_expires - time.time() + + def get_access_token(self) -> str: + """ + 获取access_token + :return: + """ + token_refresh_data = self.token_refresh() + access_token = token_refresh_data['access_token'] GLOBAL_VAR.refresh_token = self.refresh_token GLOBAL_VAR.access_token = access_token logger.debug(access_token) diff --git a/aliyunpan/api/req.py b/aliyunpan/api/req.py index 18e4a24..6cca395 100644 --- a/aliyunpan/api/req.py +++ b/aliyunpan/api/req.py @@ -1,5 +1,6 @@ import json import sys +from threading import RLock import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning @@ -27,6 +28,7 @@ def __init__(self, disk=None): return self._first_init = False self._disk = disk + self._lock = RLock() self._retry_num = 3 self._session = requests.Session() self._timeout = 5 @@ -64,13 +66,17 @@ def _req(self, method, *args, **kwargs): try: logger.debug(r.status_code) if depth: - if self._disk and not stream and json.loads(r.text)['code'] == AliyunpanCode.token_invalid: + self._lock.acquire() + if self._disk and not stream and json.loads(r.text)['code'] == AliyunpanCode.token_invalid \ + or self._disk.refresh_token_expires_sec < 600: depth -= 1 try: self._disk.access_token = self._disk.get_access_token() except InvalidRefreshToken: self._disk.login() + self._lock.release() return self._req(method, depth=depth, *args, **kwargs) + self._lock.release() else: raise InvalidAccessToken except (KeyboardInterrupt, InvalidAccessToken, LoginFailed): @@ -81,6 +87,11 @@ def _req(self, method, *args, **kwargs): pass except: logger.debug(sys.exc_info()) + finally: + try: + self._lock.release() + except RuntimeError: + pass return r except requests.exceptions.RequestException: raise diff --git a/aliyunpan/cli/cli.py b/aliyunpan/cli/cli.py index 70d9661..1b161e0 100644 --- a/aliyunpan/cli/cli.py +++ b/aliyunpan/cli/cli.py @@ -38,6 +38,7 @@ def __init__(self, init=True, *args, **kwargs): def __del__(self): self._task_config.write(GLOBAL_VAR.tasks) + self._config.update('refresh_token', self._disk.refresh_token) def init(self, config_file=None, refresh_token=None, username=None, password=None, depth=3, timeout=None, drive_id=None, album=False): @@ -95,7 +96,7 @@ def aria2_init(self, **kwargs): ) return self._aria2 - def ls(self, path, l, query=None): + def ls(self, path='root', l=False, query=None): if query: file_info_list = self._path_list.get_file_info(self._disk.search(query)) else: diff --git a/aliyunpan/cli/config.py b/aliyunpan/cli/config.py index c46be93..b873084 100644 --- a/aliyunpan/cli/config.py +++ b/aliyunpan/cli/config.py @@ -18,7 +18,7 @@ def __init__(self, config_file=None): lambda self, value: setattr(self, '_config_file', Path(value))) def read(self): - if not self._config_file.is_file(): + if not self._config_file or not self._config_file.is_file(): return {} with self.config_file.open(encoding='utf-8') as f: return yaml.load(f) or {} @@ -30,7 +30,7 @@ def write(self, conf): except FileNotFoundError: pass return - if not self._config_file.is_file(): + if not self._config_file or not self._config_file.is_file(): if not self._config_file.parent.is_dir(): self._config_file.parent.mkdir(parents=True) self._config_file.touch() @@ -41,6 +41,8 @@ def write(self, conf): return True def get(self, key): + if not self._config_file: + return False conf = self.read() try: if key in conf.keys(): @@ -51,6 +53,8 @@ def get(self, key): return None def update(self, key, value): + if not self._config_file: + return False conf = self.read() or {} if isinstance(value, DATA): value = dict(value) @@ -65,6 +69,8 @@ def update(self, key, value): return conf def delete(self, key): + if not self._config_file: + return False conf = self.read() or {} if conf and key in conf: conf.pop(key)