-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
151 lines (124 loc) · 3.67 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"
"gopkg.in/yaml.v2"
"github.com/gin-gonic/gin"
)
type QRCodeResponse struct {
Data QRCodeData `json:"data"`
}
type QRCodeData struct {
Sid string `json:"sid"`
}
type StatusResponse struct {
Status string `json:"status"`
AuthCode string `json:"authCode"`
}
type TokenResponse struct {
Data TokenData `json:"data"`
}
type TokenData struct {
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
}
type RefreshRequest struct {
RefreshToken string `json:"refresh_token"`
}
func main() {
// 请求生成二维码(不再需要,但保留逻辑)
resp, err := http.Post("http://api.extscreen.com/aliyundrive/qrcode", "application/x-www-form-urlencoded", strings.NewReader("scopes=user:base,file:all:read,file:all:write&width=500&height=500"))
if err != nil {
panic(err)
}
defer resp.Body.Close()
var qrCodeResp QRCodeResponse
if err := json.NewDecoder(resp.Body).Decode(&qrCodeResp); err != nil {
panic(err)
}
sid := qrCodeResp.Data.Sid
// 打印授权链接
fmt.Printf("点击此链接授权阿里云盘TV:https://www.aliyundrive.com/o/oauth/authorize?sid=%s\n", sid)
// 轮询检查登录状态
var refreshToken string
for {
time.Sleep(3 * time.Second)
statusResp, err := http.Get(fmt.Sprintf("https://openapi.alipan.com/oauth/qrcode/%s/status", sid))
if err != nil {
panic(err)
}
defer statusResp.Body.Close()
var statusData StatusResponse
if err := json.NewDecoder(statusResp.Body).Decode(&statusData); err != nil {
panic(err)
}
if statusData.Status == "LoginSuccess" {
authCode := statusData.AuthCode
tokenResp, err := http.Post("http://api.extscreen.com/aliyundrive/token", "application/x-www-form-urlencoded", strings.NewReader(fmt.Sprintf("code=%s", authCode)))
if err != nil {
panic(err)
}
defer tokenResp.Body.Close()
var tokenData TokenResponse
if err := json.NewDecoder(tokenResp.Body).Decode(&tokenData); err != nil {
panic(err)
}
refreshToken = tokenData.Data.RefreshToken
fmt.Printf("refresh_token: %s\n", refreshToken)
saveToken(tokenData.Data)
break
}
}
// 启动HTTP服务器
router := gin.Default()
router.POST("/refresh", func(c *gin.Context) {
var req RefreshRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
resp, err := http.Post("http://api.extscreen.com/aliyundrive/token", "application/x-www-form-urlencoded", strings.NewReader(fmt.Sprintf("refresh_token=%s", req.RefreshToken)))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer resp.Body.Close()
var tokenResp TokenResponse
if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"token_type": "Bearer",
"access_token": tokenResp.Data.AccessToken,
"refresh_token": tokenResp.Data.RefreshToken,
"expires_in": tokenResp.Data.ExpiresIn,
})
// 保存刷新后的token
saveToken(tokenResp.Data)
})
port := 5001
fmt.Printf("Server listening on port %d\n", port)
router.Run(fmt.Sprintf(":%d", port))
}
func saveToken(data TokenData) {
tokenFile, err := os.Create("token.yml")
if err != nil {
panic(err)
}
defer tokenFile.Close()
tokenData := map[string]interface{}{
"refresh_token": data.RefreshToken,
"access_token": data.AccessToken,
"expires_in": data.ExpiresIn,
}
encoder := yaml.NewEncoder(tokenFile)
if err := encoder.Encode(tokenData); err != nil {
panic(err)
}
}