Files
OpenList/alidrive/auth.go

45 lines
1.2 KiB
Go
Raw Normal View History

2020-12-24 01:39:45 +08:00
package alidrive
import (
"encoding/json"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
)
2021-02-04 10:02:34 +08:00
// refresh access_token token by refresh_token
2021-03-16 21:25:16 +08:00
func RefreshToken(drive *conf.Drive) bool {
log.Infof("刷新[%s]token...", drive.Name)
2021-03-05 21:25:44 +08:00
url := "https://websv.aliyundrive.com/token/refresh"
2021-03-16 21:25:16 +08:00
req := RefreshTokenReq{RefreshToken: drive.RefreshToken}
2020-12-24 01:39:45 +08:00
var token TokenResp
2021-03-16 21:25:16 +08:00
if body, err := DoPost(url, req, ""); err != nil {
2021-03-05 21:25:44 +08:00
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
2020-12-24 01:39:45 +08:00
return false
2021-03-05 21:25:44 +08:00
} else {
if err = json.Unmarshal(body, &token); err != nil {
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
2021-03-16 21:25:16 +08:00
log.Errorf("此处json解析失败应该是[%s]refresh_token失效", drive.Name)
2020-12-24 01:39:45 +08:00
return false
}
}
2021-03-16 21:25:16 +08:00
//刷新成功 更新token
drive.AccessToken = token.AccessToken
drive.RefreshToken = token.RefreshToken
2020-12-24 01:39:45 +08:00
return true
}
2021-03-16 21:25:16 +08:00
func RefreshTokenAll() string {
log.Infof("刷新所有token...")
res := ""
for i, drive := range conf.Conf.AliDrive.Drives {
if !RefreshToken(&conf.Conf.AliDrive.Drives[i]) {
res = res + drive.Name + ","
}
}
utils.WriteToYml(conf.ConfigFile, conf.Conf)
if res != "" {
return res[:len(res)-1]
}
return ""
}