Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Add refresh_token
Browse files Browse the repository at this point in the history
增加 token 过期的检测,与刷新 token 的机制 (fix #302 , #321 )
  • Loading branch information
yawwwwwn committed Jul 26, 2020
1 parent cdbd720 commit 093768e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
9 changes: 7 additions & 2 deletions OnlineHeart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions conf/bilibili.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ access_key =
cookie =
csrf =
uid =
refresh_token =

[types]
int = _ChatPort _protocolversion
Expand Down
47 changes: 47 additions & 0 deletions login.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,22 @@ 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
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']
}
Expand All @@ -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()

0 comments on commit 093768e

Please sign in to comment.