-
Notifications
You must be signed in to change notification settings - Fork 1
/
authorizer.go
66 lines (59 loc) · 1.51 KB
/
authorizer.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
package mp
import (
"github.com/bysir-zl/bygo/wx_open/util"
"fmt"
"errors"
"encoding/json"
"github.com/bysir-zl/bygo/wx_open"
)
type RefreshUserAccessTokenRsp struct {
wx_open.WxResponse
RefreshToken string `json:"refresh_token"`
Openid string `json:"openid"`
Scope string `json:"scope"`
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
}
func RefreshUserAccessToken(appid, refreshToken string) (r *RefreshUserAccessTokenRsp, err error) {
u := "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s"
rsp, err := util.Get(fmt.Sprintf(u, appid, refreshToken))
if err != nil {
err = errors.New("RefreshUserAccessToken err:" + err.Error())
return
}
var rs RefreshUserAccessTokenRsp
err = json.Unmarshal(rsp, &rs)
if err != nil {
return
}
err = rs.HasError()
if err != nil {
return
}
r = &rs
return
}
type AppAccessTokenRsp struct {
wx_open.WxResponse
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
}
func GetAppAccessToken(appid, appsecret string) (r *AppAccessTokenRsp, err error) {
u := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
rsp, err := util.Get(fmt.Sprintf(u, appid, appsecret))
if err != nil {
err = errors.New("GetAppAccessToken err:" + err.Error())
return
}
var rs AppAccessTokenRsp
err = json.Unmarshal(rsp, &rs)
if err != nil {
return
}
err = rs.HasError()
if err != nil {
return
}
r = &rs
return
}