Skip to content

Commit

Permalink
fix: login_v2.LoginCheck 特殊情况
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo2011 committed Feb 4, 2025
1 parent 4f0ae15 commit 3d0e395
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
20 changes: 16 additions & 4 deletions bilibili_api/data/api/login.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"method": "POST",
"verify": false,
"params": {
"sms_type": "str: loginTelCheck",
"sms_type": "str: loginTelCheck,如无 requestId 则为 secLogin",
"tmp_code": "str: 验证标记代码",
"gee_challenge": "str: 极验 challenge",
"gee_gt": "str: 极验 gt",
Expand All @@ -161,17 +161,29 @@
"params": {
"type": "(str)loginTelCheck",
"code": "(int)验证码内容",
"tmp_code": "(str)验证标记代码,来自数据处理中的解析出的参数tmp_token",
"request_id": "(str)验证请求标记,来自数据处理中的解析出的参数requestId",
"tmp_code": "(str)验证标记代码,来自数据处理中的解析出的参数 tmp_token",
"request_id": "(str)验证请求标记,来自数据处理中的解析出的参数 requestId",
"captcha_key": "(str)验证秘钥,来自申请验证码的captcha_key(data->captcha_key)"
},
"comment": "获取交换验证码"
},
"get_exchange_no_request_id": {
"url": "https://passport.bilibili.com/x/safecenter/sec/verify",
"method": "POST",
"params": {
"verify_type": "(str)sms",
"code": "(int)验证码内容",
"tmp_code": "(str)验证标记代码,来自数据处理中的解析出的参数 tmp_token",
"captcha_key": "(str)验证秘钥,来自申请验证码的captcha_key(data->captcha_key)"
},
"comment": "获取交换验证码"
},
"get_cookies": {
"url": "https://passport.bilibili.com/x/passport-login/web/exchange_cookie",
"method": "POST",
"params": {
"code": "str: 交换代码"
"code": "str: 交换代码",
"go_url": "(str)https://passport.bilibili.com/pc/passport/risk/secTip?gourl=https%3A%2F%2Fwww.bilibili.com%2F&bind_tel=1 如果无 requestId 则提供"
},
"comment": "获取 cookies (头部会执行 set-cookies)"
}
Expand Down
50 changes: 32 additions & 18 deletions bilibili_api/login_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def __init__(self, check_url: str):
self.__url = check_url
self.__yarl = yarl.URL(self.__url)
self.__token = self.__yarl.query["tmp_token"]
self.__id = self.__yarl.query["request_id"]
self.__id = self.__yarl.query.get("request_id")
self.__captcha_key = None

async def fetch_info(self) -> dict:
Expand All @@ -587,7 +587,7 @@ async def send_sms(self, geetest: Geetest) -> None:
raise GeetestException("未完成验证。")
api = API["safecenter"]["send"]
data = {
"sms_type": "loginTelCheck",
"sms_type": "loginTelCheck" if self.__id else "secLogin",
"tmp_code": self.__token,
"recaptcha_token": geetest.key,
"gee_challenge": geetest.challenge,
Expand All @@ -608,28 +608,42 @@ async def complete_check(self, code: str) -> Credential:
Returns:
Credential: 凭据类
"""
if self.__captcha_key is None:
raise LoginError("尚未发送验证码。")
api = API["safecenter"]["get_exchange"]
data = {
"type": "loginTelCheck",
"code": code,
"tmp_code": self.__token,
"request_id": self.__id,
"captcha_key": self.__captcha_key,
}
exchange_code = (await Api(**api, no_csrf=True).update_data(**data).result)[
"code"
]
exchange_url = API["safecenter"]["get_cookies"]["url"]
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0",
"Referer": "https://passport.bilibili.com/login",
"Referer": self.__url,
}
if self.__captcha_key is None:
raise LoginError("尚未发送验证码。")
if self.__id:
api = API["safecenter"]["get_exchange"]
data = {
"type": "loginTelCheck",
"code": code,
"tmp_code": self.__token,
"request_id": self.__id,
"captcha_key": self.__captcha_key,
}
else:
api = API["safecenter"]["get_exchange_no_request_id"]
data = {
"verify_type": "sms",
"tmp_code": self.__token,
"captcha_key": self.__captcha_key,
"code": code,
}
exchange_code = (
await Api(**api, no_csrf=True, headers=headers).update_data(**data).result
)["code"]
exchange_url = API["safecenter"]["get_cookies"]["url"]
exchange_data = {"code": exchange_code}
if self.__id is None:
exchange_data["go_url"] = (
"https://passport.bilibili.com/pc/passport/risk/secTip?gourl=https%3A%2F%2Fwww.bilibili.com%2F&bind_tel=1"
)
resp = await get_client().request(
method="POST",
url=exchange_url,
data={"code": exchange_code},
data=exchange_data,
headers=headers,
)
credential = Credential(
Expand Down

0 comments on commit 3d0e395

Please sign in to comment.