Skip to content

Commit

Permalink
改用 oauth2.0协议 绕过验证码 Quan666#30 Quan666#29
Browse files Browse the repository at this point in the history
  • Loading branch information
Quan666 committed May 22, 2024
1 parent e92473f commit 201e6e7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 127 deletions.
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,3 @@ PikPak API Python 实现
# Install

pip3 install pikpakapi


### 登陆验证码

如果遇到如下提示:

```bash
Please solve the captcha in the browser: https://user.mypikpak.com/captcha/v2/spritePuzzle.html?action=POST%3A%2Fv1%2Fauth%2Fsignin&appName=NONE&appid=XBASE&captcha_token=ck0.xxxxx&clientVersion=NONE&client_id=YUMx5nI8ZU8Ap8pm&creditkey=ck0.xxxx&credittype=1&device_id=20af2b71ef854c3db2780c8d3c192f9a&deviceid=20af2b71ef854c3db2780c8d3c192f9a&event=login3&mainHost=user.mypikpak.com&platformVersion=NONE&privateStyle=&traceid=&redirect_uri=https%3A%2F%2Fmypikpak.com%2Floading&state=getcaptcha1716395788716
Input the captcha_token (from the browser address bar, it's after captcha_token=):
```
将提示中的链接复制粘贴到浏览器,通过滑块验证码之后,在浏览器地址栏找到 `captcha_token` 参数到值,复制粘贴到程序中,注意不要复制错误。
例如:
```bash
https://mypikpak.com/loading?state=getcaptcha1716395599394&captcha_token=ck0.aBc6jFSxbI4qhXVsAh8jQps_mcYLRfYfu9YXv2rOaai_7MRWnKpc0MUtyOBpCo68bh_pCcBg3-GK-FCB3DYIBSBn3e1ywEkRWz4g56R3dH7UcUTs9QZGacSd3NStECx_8MG2bHZpBwmuuQVBsXokCR42uHssss_IrjfKH5nzODrgXWXJpccc9gtwJ97G7oVseuLSyGUNvMRuOoCquczH_u3b10An1vW0eHMvj_YQCg9LpFC_RW_ZXU2-DwhCjEUDfLC-x-LBLxxApk7huSFXk-pwEOcGbWnq8J-T56KCvrxJTjgYgezEVAJzVRGuH5TKY-jqJKoMR3MkVxszInM&expires_in=549
```
54 changes: 17 additions & 37 deletions pikpakapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import httpx

from .utils import device_id_generator, get_timestamp

from .PikpakException import PikpakException
from .enums import DownloadStatus
Expand Down Expand Up @@ -43,7 +42,6 @@ def __init__(
password: Optional[str] = None,
encoded_token: Optional[str] = None,
httpx_client_args: Optional[Dict[str, Any]] = {},
device_id: Optional[str] = None,
):
"""
username: str - username of the user
Expand All @@ -60,10 +58,6 @@ def __init__(
self.refresh_token = None
self.user_id = None

# device_id is used to identify the device, if not provided, a random device_id will be generated, 32 characters
self.device_id = device_id if device_id else device_id_generator()
self.captcha_token = None

self.httpx_client_args = httpx_client_args

self._path_id_cache = {}
Expand All @@ -89,19 +83,18 @@ def get_headers(self, access_token: Optional[str] = None) -> Dict[str, str]:
if access_token:
headers["Authorization"] = f"Bearer {access_token}"

if self.captcha_token:
headers["X-Captcha-Token"] = self.captcha_token
if self.device_id:
headers["X-Device-Id"] = self.device_id

return headers

async def _make_request(
self, method: str, url: str, data=None, params=None
self, method: str, url: str, data=None, params=None, headers=None
) -> Dict[str, Any]:
async with httpx.AsyncClient(**self.httpx_client_args) as client:
response = await client.request(
method, url, json=data, params=params, headers=self.get_headers()
method,
url,
json=data,
params=params,
headers=self.get_headers() if not headers else headers,
)
json_data = response.json()

Expand All @@ -125,8 +118,9 @@ async def _request_post(
self,
url: str,
data: dict = None,
headers: dict = None,
):
return await self._make_request("post", url, data=data)
return await self._make_request("post", url, data=data, headers=headers)

async def _request_patch(
self,
Expand Down Expand Up @@ -178,40 +172,26 @@ async def login(self) -> None:
"""
Login to PikPak
"""
data = await self.captcha_init()
if data.get("url"):
print(
f"Please solve the captcha in the browser: \n{data.get('url')}&redirect_uri=https%3A%2F%2Fmypikpak.com%2Floading&state=getcaptcha{get_timestamp()}"
)
# 要求通过验证码后地址栏中的 captcha_token
captcha_token = input(
"Input the captcha_token (from the browser address bar, it's after captcha_token=): \n"
)
if not captcha_token:
raise PikpakException("captcha_token is required")
self.captcha_token = captcha_token
else:
captcha_token = data.get("captcha_token")
if not captcha_token:
raise PikpakException("Get captcha token failed, please try again.")
self.captcha_token = captcha_token

login_url = f"https://{PikPakApi.PIKPAK_USER_HOST}/v1/auth/signin"
login_url = f"https://{PikPakApi.PIKPAK_USER_HOST}/v1/auth/token"
login_data = {
"client_id": self.CLIENT_ID,
"client_secret": self.CLIENT_SECRET,
"password": self.password,
"username": self.username,
"grant_type": "password",
}
user_info = await self._request_post(login_url, login_data)
user_info = await self._request_post(
login_url,
login_data,
{
"Content-Type": "application/x-www-form-urlencoded",
},
)
self.access_token = user_info["access_token"]
self.refresh_token = user_info["refresh_token"]
self.user_id = user_info["sub"]
self.encode_token()

# 清除, 暂时只用于登录
self.captcha_token = None

async def refresh_access_token(self) -> None:
"""
Refresh access token
Expand Down
73 changes: 0 additions & 73 deletions pikpakapi/utils.py

This file was deleted.

0 comments on commit 201e6e7

Please sign in to comment.