/
api_access_token.go
155 lines (129 loc) · 5.34 KB
/
api_access_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
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
152
153
154
155
package feishu
import (
"context"
"fmt"
"time"
)
// 名称: [访问凭证] 获取 app_access_token(企业自建应用)
// Func: [api_access_token.go] GetAppAccessTokenInternal
//
// 描述: 企业自建应用通过此接口获取 app_access_token,调用接口获取应用资源时,需要使用 app_access_token 作为授权凭证
// Info: token 有效期为 2 小时,在此期间调用该接口 token 不会改变。当 token 有效期小于 30 分的时候,再次请求获取 token 的时候,会生成一个新的 token,与此同时老的 token 依然有效
//
// Doc: https://open.feishu.cn/document/ukTMukTMukTM/ukDNz4SO0MjL5QzM/auth-v3/auth/app_access_token_internal
//
// 自建应用: true
// 商店应用: false
//
// HTTP URL: /open-apis/auth/v3/app_access_token/internal
// HTTP Method: POST
//
// 请求头: Content-Type=application/json; charset=utf-8
//
type appAccessTokenInternalRequest struct {
AppID string `json:"app_id"` // 应用唯一标识,创建应用后获得
AppSecret string `json:"app_secret"` // 应用秘钥,创建应用后获得
}
type appAccessTokenInternalResponse struct {
fsResponse
AppAccessTokenInternal
}
type AppAccessTokenInternal struct {
AppAccessToken string `json:"app_access_token"` // 访问 token
Expire int `json:"expire"` // app_access_token 过期时间,单位:秒
}
func (a *app) GetAppAccessTokenInternal() (AppAccessTokenInternal, error) {
return a.GetAppAccessTokenInternalWithContext(context.Background())
}
func (a *app) GetAppAccessTokenInternalWithContext(ctx context.Context) (AppAccessTokenInternal, error) {
apiDomain := "访问凭证"
apiName := "获取 app_access_token(企业自建应用)"
urlSuffix := "/open-apis/auth/v3/app_access_token/internal"
if !a.isSupported(true, false) {
return AppAccessTokenInternal{}, fmt.Errorf(_fmtErrNotSupported, apiDomain, apiName)
}
data := &appAccessTokenInternalRequest{
AppID: a.id,
AppSecret: a.secret,
}
header := map[string]string{
"Content-Type": "application/json; charset=utf-8",
}
reqID, reader, err := a._postWithContext(ctx, urlSuffix, data, a.buildOpts(apiDomain, apiName, header)...)
if err != nil {
return AppAccessTokenInternal{}, err
}
resp := new(appAccessTokenInternalResponse)
if err = a._decodeResp(apiDomain, apiName, reader, resp); err != nil {
return AppAccessTokenInternal{}, err
}
if err = resp.check(reqID, apiDomain, apiName); err != nil {
return AppAccessTokenInternal{}, err
}
a.appAccess.Lock()
defer a.appAccess.Unlock()
a.appAccess.set(resp.AppAccessToken, time.Duration(resp.Expire)*time.Second, 30*time.Minute)
return resp.AppAccessTokenInternal, nil
}
// 名称: [访问凭证] 获取 tenant_access_token(企业自建应用)
// Func: [api_access_token.go] GetTenantAccessTokenInternal
//
// 描述: 企业自建应用通过此接口获取 tenant_access_token,调用接口获取企业资源时,需要使用 tenant_access_token 作为授权凭证
// Info: token 有效期为 2 小时,在此期间调用该接口 token 不会改变。当 token 有效期小于 30 分的时候,再次请求获取 token 的时候,会生成一个新的 token,与此同时老的 token 依然有效
//
// Doc: https://open.feishu.cn/document/ukTMukTMukTM/ukDNz4SO0MjL5QzM/auth-v3/auth/tenant_access_token_internal
//
// 自建应用: true
// 商店应用: false
//
// HTTP URL: /open-apis/auth/v3/tenant_access_token/internal
// HTTP Method: POST
// 权限要求: 无
//
// 请求头: Content-Type=application/json; charset=utf-8
//
type tenantAccessTokenInternalRequest struct {
AppID string `json:"app_id"` // 应用唯一标识,创建应用后获得
AppSecret string `json:"app_secret"` // 应用秘钥,创建应用后获得
}
type tenantAccessTokenInternalResponse struct {
fsResponse
TenantAccessTokenInternal
}
type TenantAccessTokenInternal struct {
TenantAccessToken string `json:"tenant_access_token"` // 访问 token
Expire int `json:"expire"` // token 过期时间,单位: 秒
}
func (a *app) GetTenantAccessTokenInternal() (TenantAccessTokenInternal, error) {
return a.GetTenantAccessTokenInternalWithContext(context.Background())
}
func (a *app) GetTenantAccessTokenInternalWithContext(ctx context.Context) (TenantAccessTokenInternal, error) {
apiDomain := "访问凭证"
apiName := "获取 tenant_access_token(企业自建应用)"
urlSuffix := "/open-apis/auth/v3/tenant_access_token/internal"
if !a.isSupported(true, false) {
return TenantAccessTokenInternal{}, fmt.Errorf(_fmtErrNotSupported, apiDomain, apiName)
}
data := &tenantAccessTokenInternalRequest{
AppID: a.id,
AppSecret: a.secret,
}
header := map[string]string{
"Content-Type": "application/json; charset=utf-8",
}
reqID, reader, err := a._postWithContext(ctx, urlSuffix, data, a.buildOpts(apiDomain, apiName, header)...)
if err != nil {
return TenantAccessTokenInternal{}, err
}
resp := new(tenantAccessTokenInternalResponse)
if err = a._decodeResp(apiDomain, apiName, reader, resp); err != nil {
return TenantAccessTokenInternal{}, err
}
if err = resp.check(reqID, apiDomain, apiName); err != nil {
return TenantAccessTokenInternal{}, err
}
a.tenantAccess.Lock()
defer a.tenantAccess.Unlock()
a.tenantAccess.set(resp.TenantAccessToken, time.Duration(resp.Expire)*time.Second, 30*time.Minute)
return resp.TenantAccessTokenInternal, nil
}