fix qq refresh token logic & add refresh login api

This commit is contained in:
aynakeya
2025-08-01 22:59:54 +08:00
parent 111797db21
commit d82d8ba438
7 changed files with 27 additions and 8 deletions

View File

@@ -121,6 +121,7 @@ type Loginable interface {
Login(username string, password string) error
Logout() error
IsLogin() bool
RefreshLogin() error
QrLogin() (*QrLoginSession, error)
QrLoginVerify(qrlogin *QrLoginSession) (*QrLoginResult, error)
RestoreSession(session string) error

View File

@@ -19,6 +19,10 @@ func (n *Kugou) IsLogin() bool {
return ok
}
func (p *Kugou) RefreshLogin() error {
return nil
}
func (n *Kugou) QrLogin() (*miaosic.QrLoginSession, error) {
unixMili := time.Now().UnixMilli()
data := map[string]string{

View File

@@ -21,6 +21,10 @@ func (n *Netease) IsLogin() bool {
return status.Account.Id != 0
}
func (p *Netease) RefreshLogin() error {
return nil
}
func (n *Netease) QrLogin() (*miaosic.QrLoginSession, error) {
unikey, err := neteaseApi.GetQrUnikey(n.ReqData)
if err != nil {

View File

@@ -14,6 +14,9 @@ type Credential struct {
RefreshKey string `json:"refresh_key"`
EncryptUin string `json:"encryptUin"`
LoginType int `json:"loginType"`
// Extra field to track if I should refresh token
// its not in qq cookie
CreatedAt int64 `json:"created_at"`
}
func NewCredential() *Credential {

View File

@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/AynaLivePlayer/miaosic"
"time"
)
func (p *QQMusicProvider) Login(username string, password string) error {
@@ -23,6 +24,10 @@ func (p *QQMusicProvider) IsLogin() bool {
return p.cred.HasMusicID() && p.cred.HasMusicKey()
}
func (p *QQMusicProvider) RefreshLogin() error {
return p.refreshToken()
}
func (p *QQMusicProvider) refreshToken() error {
if p.cred.RefreshKey == "" || p.cred.RefreshToken == "" || !p.cred.HasMusicKey() || !p.cred.HasMusicKey() {
return errors.New("miaosic (qq): invalid credentials")
@@ -52,6 +57,7 @@ func (p *QQMusicProvider) refreshToken() error {
p.cred.RefreshKey = data.Get("data.refresh_key").String()
p.cred.EncryptUin = data.Get("data.encryptUin").String()
p.cred.LoginType = int(data.Get("data.loginType").Int())
p.cred.CreatedAt = time.Now().Unix()
return nil
}

View File

@@ -270,5 +270,6 @@ func (p *QQMusicProvider) getCredentialWithCode(code string, loginType int) (*mi
p.cred.RefreshKey = data.Get("data.refresh_key").String()
p.cred.EncryptUin = data.Get("data.encryptUin").String()
p.cred.LoginType = int(data.Get("data.loginType").Int())
p.cred.CreatedAt = time.Now().Unix()
return &miaosic.QrLoginResult{Success: true, Message: "ok"}, nil
}

View File

@@ -15,19 +15,19 @@ import (
)
func (p *QQMusicProvider) makeApiRequest(module, method string, params map[string]interface{}) (gjson.Result, error) {
expiredTime := time.UnixMilli(p.cred.CreatedAt * 1000).Add(7 * 24 * time.Hour)
if expiredTime.Before(time.Now().Add(24*time.Hour)) && !p.tokenRefreshed {
//if !p.tokenRefreshed {
// only refresh once
p.tokenRefreshed = true
p.qimeiUpdated = false
_ = p.refreshToken()
}
if !p.qimeiUpdated {
_, _ = getQimei(p.device, p.cfg.Version)
p.qimeiUpdated = true
}
expiredTime := time.UnixMilli(p.cred.ExpiredAt * 1000)
if expiredTime.Before(time.Now().Add(24*time.Hour)) && !p.tokenRefreshed {
_ = p.refreshToken()
// only refresh once
p.tokenRefreshed = true
}
// 公共参数
common := map[string]interface{}{
"ct": "11",