/
cgi-bin.component.api_query_auth.go
55 lines (50 loc) · 2.93 KB
/
cgi-bin.component.api_query_auth.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
package wechatopen
import (
"context"
"go.dtapp.net/gojson"
"go.dtapp.net/gorequest"
"net/http"
)
type CgiBinComponentApiQueryAuthResponse struct {
AuthorizationInfo struct {
AuthorizerAppid string `json:"authorizer_appid"` // 授权方 appid
AuthorizerAccessToken string `json:"authorizer_access_token"` // 接口调用令牌(在授权的公众号/小程序具备 API 权限时,才有此返回值)
ExpiresIn int64 `json:"expires_in"` // authorizer_access_token 的有效期(在授权的公众号/小程序具备API权限时,才有此返回值),单位:秒
AuthorizerRefreshToken string `json:"authorizer_refresh_token"` // 刷新令牌(在授权的公众号具备API权限时,才有此返回值),刷新令牌主要用于第三方平台获取和刷新已授权用户的 authorizer_access_token。一旦丢失,只能让用户重新授权,才能再次拿到新的刷新令牌。用户重新授权后,之前的刷新令牌会失效
FuncInfo []struct {
FuncscopeCategory struct {
Id int `json:"id"`
} `json:"funcscope_category"`
ConfirmInfo struct {
NeedConfirm int `json:"need_confirm"`
AlreadyConfirm int `json:"already_confirm"`
CanConfirm int `json:"can_confirm"`
} `json:"confirm_info,omitempty"`
} `json:"func_info"`
} `json:"authorization_info"`
}
type CgiBinComponentApiQueryAuthResult struct {
Result CgiBinComponentApiQueryAuthResponse // 结果
Body []byte // 内容
Http gorequest.Response // 请求
}
func newCgiBinComponentApiQueryAuthResult(result CgiBinComponentApiQueryAuthResponse, body []byte, http gorequest.Response) *CgiBinComponentApiQueryAuthResult {
return &CgiBinComponentApiQueryAuthResult{Result: result, Body: body, Http: http}
}
// CgiBinComponentApiQueryAuth 使用授权码获取授权信息
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/authorization_info.html
func (c *Client) CgiBinComponentApiQueryAuth(ctx context.Context, componentAccessToken, authorizationCode string, notMustParams ...gorequest.Params) (*CgiBinComponentApiQueryAuthResult, error) {
// 参数
params := gorequest.NewParamsWith(notMustParams...)
params.Set("component_appid", c.config.componentAppId) // 第三方平台appid
params.Set("authorization_code", authorizationCode) // 授权码会在授权成功时返回给第三方平台
// 请求
request, err := c.request(ctx, apiUrl+"/cgi-bin/component/api_query_auth?component_access_token="+componentAccessToken, params, http.MethodPost)
if err != nil {
return newCgiBinComponentApiQueryAuthResult(CgiBinComponentApiQueryAuthResponse{}, request.ResponseBody, request), err
}
// 定义
var response CgiBinComponentApiQueryAuthResponse
err = gojson.Unmarshal(request.ResponseBody, &response)
return newCgiBinComponentApiQueryAuthResult(response, request.ResponseBody, request), err
}