From 138aa27dd65bcc2a541fd822f1144972fb8603b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Wed, 8 Feb 2023 17:07:11 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Request=20Full=20Access=20Coo?= =?UTF-8?q?kie=20Token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/apihelper/client/components/signin.py | 26 ++++++++++++++++++- plugins/genshin/cookies.py | 20 ++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/modules/apihelper/client/components/signin.py b/modules/apihelper/client/components/signin.py index d036a65..2756d11 100644 --- a/modules/apihelper/client/components/signin.py +++ b/modules/apihelper/client/components/signin.py @@ -26,11 +26,12 @@ class SignIn: QRCODE_GET_API = "https://hk4e-sdk.mihoyo.com/hk4e_cn/combo/panda/qrcode/query" GAME_TOKEN_API = "https://api-takumi.mihoyo.com/auth/api/getCookieAccountInfoByGameToken" GAME_LTOKEN_API = "https://passport-api.mihoyo.com/account/ma-cn-session/app/getTokenByGameToken" + COOKIES_GET_API = "https://passport-api.mihoyo.com/account/auth/api/getCookieAccountInfoBySToken" def __init__(self, uid: int = 0, cookie: Dict = None): self.client = AsyncClient() self.uid = uid - self.cookie = cookie if cookie is not None else {} + self.cookie = cookie.copy() if cookie is not None else {} self.parse_uid() self.ticket = None self.device_id = None @@ -137,6 +138,29 @@ class SignIn: if res_data.get("stat", "") == "Confirmed": return await self.set_cookie(res_json.get("data", {})) + async def get_cookie_account_info_by_stoken(self, stoken, uid): + headers = { + "x-rpc-app_version": "2.11.1", + "User-Agent": ( + "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) " + "AppleWebKit/605.1.15 (KHTML, like Gecko) miHoYoBBS/2.11.1" + ), + "x-rpc-client_type": "5", + "Referer": "https://webstatic.mihoyo.com/", + "Origin": "https://webstatic.mihoyo.com", + } + params = { + "stoken": stoken, + "uid": uid, + } + res = await self.client.get( + self.COOKIES_GET_API, + headers=headers, + params=params, + ) + res_json = res.json() + return res_json.get("data", {}).get("cookie_token", "") + @staticmethod def generate_qrcode(url: str) -> bytes: qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4) diff --git a/plugins/genshin/cookies.py b/plugins/genshin/cookies.py index 13ebf6a..af43a2a 100644 --- a/plugins/genshin/cookies.py +++ b/plugins/genshin/cookies.py @@ -156,13 +156,11 @@ class SetUserCookies(Plugin.Conversation, BasePlugin.Conversation): "关于如何获取Cookies\n" "现在因为网站HttpOnly策略无法通过脚本获取,因此操作只能在PC上运行。\n\n" "PC:\n" - "1、打开米游社并登录\n" - "2、打开通行证并登录\n" - "3、登录完成后刷新米游社和通行证网页\n" - "4、进入通行证按F12打开开发者工具\n" - "5、将开发者工具切换至网络(Network)并点击过滤栏中的文档(Document)并刷新页面\n" - "6、在请求列表中选择第一个并点击\n" - "7、找到并复制请求标头(Request Headers)中的Cookie\n" + "1、打开通行证并登录\n" + "2、进入通行证按F12打开开发者工具\n" + "3、将开发者工具切换至网络(Network)并点击过滤栏中的文档(Document)并刷新页面\n" + "4、在请求列表中选择第一个并点击\n" + "5、找到并复制请求标头(Request Headers)中的Cookie\n" "如发现没有请求标头(Request Headers)大概因为缓存的存在需要你点击禁用缓存(Disable Cache)再次刷新页面" ) else: @@ -277,7 +275,13 @@ class SetUserCookies(Plugin.Conversation, BasePlugin.Conversation): sign_in_client = SignIn(cookie=add_user_command_data.cookies) await sign_in_client.get_s_token() add_user_command_data.cookies = sign_in_client.cookie - logger.info("用户 %s[%s] 绑定时获取 stoken 成功", user.full_name, user.id) + logger.success("用户 %s[%s] 绑定时获取 stoken 成功", user.full_name, user.id) + stoken = add_user_command_data.cookies.get("stoken") + account_id = add_user_command_data.cookies.get("account_id") + if stoken and account_id: + cookie_token = await sign_in_client.get_cookie_account_info_by_stoken(stoken, account_id) + add_user_command_data.cookies["cookie_token"] = cookie_token + logger.success("用户 %s[%s] 绑定时获取 cookie_token 成功", user.full_name, user.id) user_info: Optional[GenshinAccount] = None level: int = 0 # todo : 多账号绑定