-
Notifications
You must be signed in to change notification settings - Fork 4
/
ali_open.go
138 lines (130 loc) · 3.4 KB
/
ali_open.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
package alist
import (
"fmt"
"strings"
"api.nn.ci/apps/common"
"api.nn.ci/utils"
"github.com/gin-gonic/gin"
)
var (
aliClientID string
aliClientSecret string
aliMinutes int
aliMax int
)
type AliAccessTokenReq struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
GrantType string `json:"grant_type"`
Code string `json:"code"`
RefreshToken string `json:"refresh_token"`
}
type AliAccessTokenErr struct {
Code string `json:"code"`
Message string `json:"message"`
Error string `json:"error"`
}
func aliAccessToken(c *gin.Context) {
var req AliAccessTokenReq
err := c.ShouldBind(&req)
if err != nil {
common.ErrorJson(c, AliAccessTokenErr{
Code: "InternalError",
Message: err.Error(),
Error: err.Error(),
})
return
}
if req.ClientID == "" {
req.ClientID = aliClientID
req.ClientSecret = aliClientSecret
}
if req.GrantType != "authorization_code" && req.GrantType != "refresh_token" {
common.ErrorJson(c, AliAccessTokenErr{
Code: "Invalid request",
Message: "Incorrect GrantType",
Error: "Incorrect GrantType",
}, 400)
return
}
if len(req.RefreshToken) == 32 {
common.ErrorJson(c, AliAccessTokenErr{
Code: "Invalid request",
Message: "You should use the token that request with aliyundrive open insted of aliyundrive",
Error: "You should use the token that request with aliyundrive open insted of aliyundrive",
}, 400)
return
}
if req.GrantType == "authorization_code" && req.Code == "" {
common.ErrorJson(c, AliAccessTokenErr{
Code: "Invalid request",
Message: "Code missed",
Error: "Code missed",
}, 400)
return
}
if req.GrantType == "refresh_token" && strings.Count(req.RefreshToken, ".") != 2 {
common.ErrorJson(c, AliAccessTokenErr{
Code: "Invalid request",
Message: "Incorrect refresh_token or missed",
Error: "Incorrect refresh_token or missed",
}, 400)
return
}
var e AliAccessTokenErr
res, err := utils.RestyClient.R().SetBody(req).SetError(&e).Post("https://openapi.aliyundrive.com/oauth/access_token")
if err != nil {
common.ErrorJson(c, AliAccessTokenErr{
Code: "InternalError",
Message: err.Error(),
Error: err.Error(),
})
return
}
if e.Code != "" {
e.Error = fmt.Sprintf("%s: %s", e.Code, e.Message)
common.ErrorJson(c, e, res.StatusCode())
return
}
common.JsonBytes(c, res.Body())
}
type aliQrcodeReq struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Scopes []string `json:"scopes"`
}
func aliQrcode(c *gin.Context) {
var req aliQrcodeReq
err := c.ShouldBind(&req)
if err != nil {
c.JSON(500, AliAccessTokenErr{
Code: "InternalError",
Message: err.Error(),
Error: err.Error(),
})
return
}
if req.ClientID == "" {
req.ClientID = aliClientID
req.ClientSecret = aliClientSecret
}
if req.Scopes == nil || len(req.Scopes) == 0 {
req.Scopes = []string{"user:base", "file:all:read", "file:all:write"}
}
var e AliAccessTokenErr
res, err := utils.RestyClient.R().SetBody(req).SetError(&e).Post("https://openapi.aliyundrive.com/oauth/authorize/qrcode")
if err != nil {
c.JSON(500, AliAccessTokenErr{
Code: "InternalError",
Message: err.Error(),
Error: err.Error(),
})
return
}
if e.Code != "" {
e.Error = fmt.Sprintf("%s: %s", e.Code, e.Message)
c.JSON(res.StatusCode(), e)
return
}
common.JsonBytes(c, res.Body())
}