From 093768ee4ceeb09cec80232a4e7978db07b38d62 Mon Sep 17 00:00:00 2001 From: yawwwwwn <40122222+yawwwwwn@users.noreply.github.com> Date: Sun, 26 Jul 2020 14:34:49 +0900 Subject: [PATCH] Add refresh_token MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 token 过期的检测,与刷新 token 的机制 (fix #302 , #321 ) --- OnlineHeart.py | 9 +++++++-- conf/bilibili.conf | 1 + login.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/OnlineHeart.py b/OnlineHeart.py index 43011ba9..42e2d4e4 100644 --- a/OnlineHeart.py +++ b/OnlineHeart.py @@ -19,7 +19,7 @@ def CurrentTime(): class OnlineHeart: async def apppost_heartbeat(self): - await bilibili().apppost_heartbeat() + return await bilibili().apppost_heartbeat() async def pcpost_heartbeat(self): response = await bilibili().pcpost_heartbeat() @@ -87,7 +87,12 @@ async def run(self): if json_response['code'] in [3, -101]: Printer().printer(f"cookie过期,将重新登录","Error","red") login().login() - await self.apppost_heartbeat() + response1 = await self.apppost_heartbeat() + json_response1 = await response1.json(content_type=None) + if json_response1['code'] == -101: + # '{"code":-101,"message":"账号未登录","ttl":1}' + Printer().printer(f"token过期,尝试刷新", "Error", "red") + login().refresh_token() await self.heart_gift() await self.draw_lottery() await asyncio.sleep(300) diff --git a/conf/bilibili.conf b/conf/bilibili.conf index 18fc09b7..d82e97b5 100644 --- a/conf/bilibili.conf +++ b/conf/bilibili.conf @@ -44,6 +44,7 @@ access_key = cookie = csrf = uid = +refresh_token = [types] int = _ChatPort _protocolversion diff --git a/login.py b/login.py index 57027964..4093ca6b 100644 --- a/login.py +++ b/login.py @@ -129,12 +129,14 @@ def login(self): break try: access_key = response.json()['data']['token_info']['access_token'] + refresh_token = response.json()['data']['token_info']['refresh_token'] cookie = response.json()['data']['cookie_info']['cookies'] cookie_format = "" for i in range(len(cookie)): cookie_format = cookie_format + cookie[i]['name'] + "=" + cookie[i]['value'] + ";" bilibili().dic_bilibili['csrf'] = cookie[0]['value'] bilibili().dic_bilibili['access_key'] = access_key + bilibili().dic_bilibili['refresh_token'] = refresh_token bilibili().dic_bilibili['cookie'] = cookie_format bilibili().dic_bilibili['uid'] = cookie[1]['value'] bilibili().dic_bilibili['pcheaders']['cookie'] = cookie_format @@ -142,6 +144,7 @@ def login(self): dic_saved_session = { 'csrf': cookie[0]['value'], 'access_key': access_key, + 'refresh_token': refresh_token, 'cookie': cookie_format, 'uid': cookie[1]['value'] } @@ -156,3 +159,47 @@ async def login_new(self): bilibili().load_session(bilibili().dic_bilibili['saved-session']) else: return self.login() + + def refresh_token(self): + url = "https://passport.bilibili.com/api/v2/oauth2/refresh_token" + params_dic = { + "access_token": bilibili().dic_bilibili["access_key"], + "actionKey": bilibili().dic_bilibili["actionKey"], + "appkey": bilibili().dic_bilibili["appkey"], + "build": bilibili().dic_bilibili["build"], + "device": bilibili().dic_bilibili["device"], + "mobi_app": bilibili().dic_bilibili["mobi_app"], + "platform": bilibili().dic_bilibili["platform"], + 'refresh_token': bilibili().dic_bilibili["refresh_token"], + } + temp_params = '&'.join([f'{key}={value}' for key, value in params_dic.items()]) + sign = bilibili().calc_sign(temp_params) + payload = f'{temp_params}&sign={sign}' + response = requests.post(url, params=payload, headers=app_headers) + json_response = response.json() + if json_response["code"] == 0: + access_key = json_response['data']['token_info']['access_token'] + refresh_token = json_response['data']['token_info']['refresh_token'] + cookie = json_response['data']['cookie_info']['cookies'] + cookie_format = "" + for i in range(len(cookie)): + cookie_format = cookie_format + cookie[i]['name'] + "=" + cookie[i]['value'] + ";" + bilibili().dic_bilibili['csrf'] = cookie[0]['value'] + bilibili().dic_bilibili['access_key'] = access_key + bilibili().dic_bilibili['refresh_token'] = refresh_token + bilibili().dic_bilibili['cookie'] = cookie_format + bilibili().dic_bilibili['uid'] = cookie[1]['value'] + bilibili().dic_bilibili['pcheaders']['cookie'] = cookie_format + bilibili().dic_bilibili['appheaders']['cookie'] = cookie_format + dic_saved_session = { + 'csrf': cookie[0]['value'], + 'access_key': access_key, + 'refresh_token': refresh_token, + 'cookie': cookie_format, + 'uid': cookie[1]['value'] + } + configloader.write2bilibili(dic_saved_session) + Printer().printer(f"token刷新成功", "Info", "green") + else: + Printer().printer(f"token刷新失败,将重新登录 {json_response}", "Info", "green") + self.login()