-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTokenApi.go
83 lines (66 loc) · 1.9 KB
/
TokenApi.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
package feishuapi
import "github.com/sirupsen/logrus"
type LoginSession struct {
OpenId string
EmployeeId string
}
type UserAccessToken struct {
Access_token string
Name string
Refresh_token string
User_id string
Open_id string
}
// Create a new LoginSession
func NewLoginSession(data map[string]any) *LoginSession {
return &LoginSession{
OpenId: data["open_id"].(string),
EmployeeId: data["employee_id"].(string),
}
}
// Create a new UserAccessToken
func NewUserAccessToken(data map[string]any) *UserAccessToken {
return &UserAccessToken{
Access_token: data["access_token"].(string),
Name: data["name"].(string),
Refresh_token: data["refresh_token"].(string),
User_id: getInMap(data, "user_id", "").(string),
Open_id: data["open_id"].(string),
}
}
// Get the login session by login_token
func (c AppClient) GetLoginSession(login_token string) *LoginSession {
body := make(map[string]string)
body["code"] = login_token
resp := c.Request("post", "open-apis/mina/v2/tokenLoginValidate", nil, nil, body)
if resp == nil {
logrus.Error("nil login session return")
return nil
}
return NewLoginSession(resp)
}
// Get the UserAccessToken
func (c *AppClient) GetUserAccessToken(code string) *UserAccessToken {
u := "open-apis/authen/v1/access_token"
body := make(map[string]string)
body["grant_type"] = "authorization_code"
body["code"] = code
resp := c.Request("post", u, nil, nil, body)
if resp == nil {
logrus.Error("nil user access token return")
return nil
}
return NewUserAccessToken(resp)
}
func (c *AppClient) GetCode(redirectURL string, appID string) string {
u := "open-apis/authen/v1/index"
query := make(map[string]any)
query["redirect_uri"] = redirectURL
query["app_id"] = appID
resp := c.Request("get", u, query, nil, nil)
if resp == nil {
logrus.Error("nil code return")
return ""
}
return resp["code"].(string)
}