-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathclient.go
310 lines (252 loc) · 7.6 KB
/
client.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2020 FastWeGo
//
// 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 offiaccount
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
var (
WXServerUrl = "https://api.weixin.qq.com" // 微信 api 服务器地址
UserAgent = "fastwego/offiaccount"
ErrorAccessTokenExpire = errors.New("access token expire")
ErrorSystemBusy = errors.New("system busy")
)
/*
HttpClient 用于向公众号接口发送请求
*/
type Client struct {
Ctx *OffiAccount
}
// HTTPGet GET 请求
func (client *Client) HTTPGet(uri string) (resp []byte, err error) {
newUrl, err := client.applyAccessToken(uri)
if err != nil {
return
}
req, err := http.NewRequest(http.MethodGet, WXServerUrl+newUrl, nil)
if err != nil {
return
}
return client.httpDo(req)
}
//HTTPPost POST 请求
func (client *Client) HTTPPost(uri string, payload io.Reader, contentType string) (resp []byte, err error) {
newUrl, err := client.applyAccessToken(uri)
if err != nil {
return
}
req, err := http.NewRequest(http.MethodPost, WXServerUrl+newUrl, payload)
if err != nil {
return
}
req.Header.Add("Content-Type", contentType)
return client.httpDo(req)
}
//httpDo 执行 请求
func (client *Client) httpDo(req *http.Request) (resp []byte, err error) {
req.Header.Add("User-Agent", UserAgent)
if client.Ctx.Logger != nil {
client.Ctx.Logger.Printf("%s %s Headers %v", req.Method, req.URL.String(), req.Header)
}
response, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer response.Body.Close()
resp, err = responseFilter(response)
// 发现 access_token 过期
if err == ErrorAccessTokenExpire {
// 主动 通知 access_token 过期
err = client.Ctx.AccessToken.NoticeAccessTokenExpireHandler(client.Ctx)
if err != nil {
return
}
// 通知到位后 access_token 会被刷新,那么可以 retry 了
var accessToken string
accessToken, err = client.Ctx.AccessToken.GetAccessTokenHandler(client.Ctx)
if err != nil {
return
}
// 换新
q := req.URL.Query()
q.Set("access_token", accessToken)
req.URL.RawQuery = q.Encode()
if client.Ctx.Logger != nil {
client.Ctx.Logger.Printf("%v retry %s %s Headers %v", ErrorAccessTokenExpire, req.Method, req.URL.String(), req.Header)
}
response, err = http.DefaultClient.Do(req)
if err != nil {
return
}
defer response.Body.Close()
resp, err = responseFilter(response)
}
// -1 系统繁忙,此时请开发者稍候再试
// 重试一次
if err == ErrorSystemBusy {
if client.Ctx.Logger != nil {
client.Ctx.Logger.Printf("%v : retry %s %s Headers %v", ErrorSystemBusy, req.Method, req.URL.String(), req.Header)
}
response, err = http.DefaultClient.Do(req)
if err != nil {
return
}
defer response.Body.Close()
resp, err = responseFilter(response)
}
return
}
/*
在请求地址上附加上 access_token
*/
func (client *Client) applyAccessToken(oldUrl string) (newUrl string, err error) {
accessToken, err := client.Ctx.AccessToken.GetAccessTokenHandler(client.Ctx)
if err != nil {
return
}
if strings.Contains(oldUrl, "?") {
newUrl = oldUrl + "&access_token=" + accessToken
} else {
newUrl = oldUrl + "?access_token=" + accessToken
}
return
}
/*
筛查微信 api 服务器响应,判断以下错误:
- http 状态码 不为 200
- 接口响应错误码 errcode 不为 0
*/
func responseFilter(response *http.Response) (resp []byte, err error) {
if response.StatusCode != http.StatusOK {
err = fmt.Errorf("Status %s", response.Status)
return
}
resp, err = ioutil.ReadAll(response.Body)
if err != nil {
return
}
errorResponse := struct {
Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"`
}{}
err = json.Unmarshal(resp, &errorResponse)
if err != nil {
return
}
// 40001(覆盖刷新超过5min后,使用旧 access_token 报错) 获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口
// 42001(超过 7200s 后 报错) - access_token 超时,请检查 access_token 的有效期,请参考基础支持 - 获取 access_token 中,对 access_token 的详细机制说明
if errorResponse.Errcode == 42001 || errorResponse.Errcode == 40001 {
err = ErrorAccessTokenExpire
return
}
// -1 系统繁忙,此时请开发者稍候再试
if errorResponse.Errcode == -1 {
err = ErrorSystemBusy
return
}
if errorResponse.Errcode != 0 {
err = errors.New(string(resp))
return
}
return
}
// 防止多个 goroutine 并发刷新冲突
var refreshAccessTokenLock sync.Mutex
/*
从 公众号实例 的 AccessToken 管理器 获取 access_token
如果没有 access_token 或者 已过期,那么刷新
获得新的 access_token 后 过期时间设置为 0.9 * expiresIn 提供一定冗余
*/
func GetAccessToken(ctx *OffiAccount) (accessToken string, err error) {
accessToken, err = ctx.AccessToken.Cache.Fetch(ctx.Config.Appid)
if accessToken != "" {
return
}
refreshAccessTokenLock.Lock()
defer refreshAccessTokenLock.Unlock()
accessToken, err = ctx.AccessToken.Cache.Fetch(ctx.Config.Appid)
if accessToken != "" {
return
}
accessToken, expiresIn, err := refreshAccessTokenFromWXServer(ctx.Config.Appid, ctx.Config.Secret)
if err != nil {
return
}
// 本地缓存 access_token
d := time.Duration(expiresIn) * time.Second
_ = ctx.AccessToken.Cache.Save(ctx.Config.Appid, accessToken, d)
if ctx.Logger != nil {
ctx.Logger.Printf("%s %s %d\n", "refreshAccessTokenFromWXServer", accessToken, expiresIn)
}
return
}
/*
NoticeAccessTokenExpire 只需将本地存储的 access_token 删除,即完成了 access_token 已过期的 主动通知
retry 请求的时候,会发现本地没有 access_token ,从而触发refresh
*/
func NoticeAccessTokenExpire(ctx *OffiAccount) (err error) {
if ctx.Logger != nil {
ctx.Logger.Println("NoticeAccessTokenExpire")
}
err = ctx.AccessToken.Cache.Delete(ctx.Config.Appid)
return
}
/*
从微信服务器获取新的 AccessToken
See: https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
*/
func refreshAccessTokenFromWXServer(appid string, secret string) (accessToken string, expiresIn int, err error) {
params := url.Values{}
params.Add("appid", appid)
params.Add("secret", secret)
params.Add("grant_type", "client_credential")
url := WXServerUrl + "/cgi-bin/token?" + params.Encode()
response, err := http.Get(url)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
err = fmt.Errorf("GET %s RETURN %s", url, response.Status)
return
}
resp, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}
var result = struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
Errcode float64 `json:"errcode"`
Errmsg string `json:"errmsg"`
}{}
err = json.Unmarshal(resp, &result)
if err != nil {
err = fmt.Errorf("Unmarshal error %s", string(resp))
return
}
if result.AccessToken == "" {
err = fmt.Errorf("%s", string(resp))
return
}
return result.AccessToken, result.ExpiresIn, nil
}