fix: Solve the problem of failed binding and synchronization of Ali Pan (#9195)

This commit is contained in:
ssongliu 2025-06-20 11:12:49 +08:00 committed by GitHub
parent eddc1b6a8d
commit e3e2006c19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 3 deletions

View file

@ -497,6 +497,7 @@ func (a *aliClient) completeUpload(uploadID, fileID string) error {
type tokenResp struct {
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
}
func loadToken(refresh_token string) (string, error) {
@ -524,7 +525,7 @@ func loadToken(refresh_token string) (string, error) {
if err := json.Unmarshal(resp.Body(), &respItem); err != nil {
return "", err
}
return respItem.RefreshToken, nil
return respItem.AccessToken, nil
}
func RefreshALIToken(varMap map[string]interface{}) (string, error) {

View file

@ -284,7 +284,15 @@ func (u *BackupService) RefreshToken(req dto.OperateByName) error {
varsItem, _ := json.Marshal(varMap)
backup.Vars = string(varsItem)
return backupRepo.Save(&backup)
if err := backupRepo.Save(&backup); err != nil {
return err
}
go func() {
if err := xpack.Sync(constant.SyncBackupAccounts); err != nil {
global.LOG.Errorf("sync backup account to node failed, err: %v", err)
}
}()
return nil
}
func (u *BackupService) NewClient(backup *model.BackupAccount) (cloud_storage.CloudStorageClient, error) {

View file

@ -20,9 +20,10 @@ type aliClient struct {
}
func NewALIClient(vars map[string]interface{}) (*aliClient, error) {
refresh_token := loadParamFromVars("refresh_token", vars)
drive_id := loadParamFromVars("drive_id", vars)
token, err := RefreshALIToken(vars)
token, err := loadToken(refresh_token)
if err != nil {
return nil, err
}
@ -355,6 +356,35 @@ func (a *aliClient) completeUpload(uploadID, fileID string) error {
type tokenResp struct {
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
}
func loadToken(refresh_token string) (string, error) {
client := resty.New()
client.SetTLSClientConfig(&tls.Config{
InsecureSkipVerify: true,
})
data := map[string]interface{}{
"grant_type": "refresh_token",
"refresh_token": refresh_token,
}
url := "https://api.aliyundrive.com/token/refresh"
resp, err := client.R().
SetBody(data).
Post(url)
if err != nil {
return "", fmt.Errorf("load account token failed, err: %v", err)
}
if resp.StatusCode() != 200 {
return "", fmt.Errorf("load account token failed, code: %v", resp.StatusCode())
}
var respItem tokenResp
if err := json.Unmarshal(resp.Body(), &respItem); err != nil {
return "", err
}
return respItem.AccessToken, nil
}
func RefreshALIToken(varMap map[string]interface{}) (string, error) {