This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
forked from chyroc/go-aliyundrive
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrefresh_token.go
99 lines (88 loc) · 3.12 KB
/
refresh_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright 2022 chyroc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package aliyundrive
import (
"context"
"fmt"
"net/http"
"strings"
"time"
)
type RefreshTokenReq struct {
GrantType string `json:"grant_type"`
RefreshToken string `json:"refresh_token"`
}
type RefreshTokenResp struct {
DefaultSboxDriveID string `json:"default_sbox_drive_id"`
Role string `json:"role"`
DeviceID string `json:"device_id"`
UserName string `json:"user_name"`
NeedLink bool `json:"need_link"`
ExpireTime time.Time `json:"expire_time"`
PinSetup bool `json:"pin_setup"`
NeedRpVerify bool `json:"need_rp_verify"`
Avatar string `json:"avatar"`
UserData struct {
// DingDingRobotURL string `json:"DingDingRobotUrl"`
// EncourageDesc string `json:"EncourageDesc"`
// FeedBackSwitch bool `json:"FeedBackSwitch"`
// FollowingDesc string `json:"FollowingDesc"`
DingDingRobotURL string `json:"ding_ding_robot_url"`
EncourageDesc string `json:"encourage_desc"`
FeedBackSwitch bool `json:"feed_back_switch"`
FollowingDesc string `json:"following_desc"`
} `json:"user_data"`
TokenType string `json:"token_type"`
AccessToken string `json:"access_token"`
DefaultDriveID string `json:"default_drive_id"`
RefreshToken string `json:"refresh_token"`
IsFirstLogin bool `json:"is_first_login"`
UserID string `json:"user_id"`
NickName string `json:"nick_name"`
ExistLink []interface{} `json:"exist_link"`
State string `json:"state"`
ExpiresIn int `json:"expires_in"`
Status string `json:"status"`
}
func (r *AliyunDrive) RefreshToken(ctx context.Context, request *RefreshTokenReq) (*RefreshTokenResp, error) {
var result RefreshTokenResp
if strings.TrimSpace(request.RefreshToken) == "" {
return nil, fmt.Errorf("refresh token is empty")
}
request.GrantType = "refresh_token"
response, err := r.request(ctx, &config{
Method: http.MethodPost,
URL: "https://auth.aliyundrive.com/v2/account/token",
Body: request,
}, &result)
if err != nil {
return nil, err
}
if !response.IsSuccess() {
return nil, fmt.Errorf("%s", response.Status())
}
// save token information
if r.store != nil {
if err := r.store.Set(ctx, KeyAccessToken, []byte(result.AccessToken)); err != nil {
return nil, err
}
if err := r.store.Set(ctx, KeyRefreshToken, []byte(result.RefreshToken)); err != nil {
return nil, err
}
}
r.accessToken = result.AccessToken
return &result, nil
}