From 23a5d8aa17144a59fc429a4afae7bff47f976a65 Mon Sep 17 00:00:00 2001 From: 52funny <52funny@52funnydeMac-Pro.local> Date: Fri, 16 Aug 2024 10:00:02 +0800 Subject: [PATCH] fix: updated the login api error --- internal/pikpak/pikpak.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/internal/pikpak/pikpak.go b/internal/pikpak/pikpak.go index 64a735d..bb28e8d 100644 --- a/internal/pikpak/pikpak.go +++ b/internal/pikpak/pikpak.go @@ -59,17 +59,23 @@ func NewPikPak(account, password string) PikPak { } func (p *PikPak) Login() error { + captchaToken, err := p.getCaptchaToken() + if err != nil { + return err + } m := make(map[string]string) m["client_id"] = clientID m["client_secret"] = clientSecret m["grant_type"] = "password" m["username"] = p.Account m["password"] = p.Password + m["captcha_token"] = captchaToken bs, err := jsoniter.Marshal(&m) if err != nil { return err } - req, err := http.NewRequest("POST", "https://user.mypikpak.com/v1/auth/token", bytes.NewBuffer(bs)) + + req, err := http.NewRequest("POST", "https://user.mypikpak.com/v1/auth/signin", bytes.NewBuffer(bs)) if err != nil { return err } @@ -92,6 +98,34 @@ func (p *PikPak) Login() error { return nil } +func (p *PikPak) getCaptchaToken() (string, error) { + m := make(map[string]any) + m["client_id"] = clientID + m["device_id"] = p.DeviceId + m["action"] = "POST:https://user.mypikpak.com/v1/auth/signin" + m["meta"] = map[string]string{ + "username": p.Account, + } + body, err := jsoniter.Marshal(&m) + if err != nil { + return "", err + } + req, err := http.NewRequest("POST", "https://user.mypikpak.com/v1/shield/captcha/init", bytes.NewBuffer(body)) + if err != nil { + return "", err + } + req.Header.Add("Content-Type", "application/json") + bs, err := p.sendRequest(req) + if err != nil { + return "", err + } + error_code := jsoniter.Get(bs, "error_code").ToInt() + if error_code != 0 { + return "", fmt.Errorf("get captcha error: %v", jsoniter.Get(bs, "error").ToString()) + } + return jsoniter.Get(bs, "captcha_token").ToString(), nil +} + func (p *PikPak) sendRequest(req *http.Request) ([]byte, error) { p.setHeader(req) resp, err := p.client.Do(req)