123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- /**
- * Copyright (c) 2022 Yansen Zhang
- * wxcomponent is licensed under Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2.
- * You may obtain a copy of Mulan PSL v2 at:
- * http://license.coscl.org.cn/MulanPSL2
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- * See the Mulan PSL v2 for more details.
- **/
-
- package authorization
-
- import (
- "encoding/json"
- "net/url"
-
- "gitee.com/yansen_zh/wxcomponent/config"
- "gitee.com/yansen_zh/wxcomponent/errors"
- "gitee.com/yansen_zh/wxcomponent/utils/request"
- )
-
- // AuthorizerTokenResult 获取/刷新接口调用令牌结果
- type AuthorizerTokenResult struct {
- errors.Error
- AuthorizerAccessToken string `json:"authorizer_access_token"`
- ExpiresIn int `json:"expires_in"`
- AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
- }
-
- const (
- apiAuthorizerToken = "https://apies.weixin.qq.com/cgi-bin/component/api_authorizer_token"
- )
-
- // RefreshAuthorizerToken 获取/刷新接口调用令牌
- func RefreshAuthorizerToken(authorizerAppID, authorizerRefreshToken string) (*AuthorizerTokenResult, error) {
- if authorizerAppID == "" {
- return nil, errors.New("获取/刷新接口调用令牌 授权方 appid 不能为空")
- }
-
- if authorizerRefreshToken == "" {
- return nil, errors.New("获取/刷新接口调用令牌 刷新令牌 不能为空")
- }
-
- queryParam := url.Values{}
- queryParam.Set("component_access_token", config.GetAccessToken())
-
- data := map[string]string{
- "component_appid": config.GetAppID(),
- "authorizer_appid": authorizerAppID,
- "authorizer_refresh_token": authorizerRefreshToken,
- }
-
- resp, e2 := request.PostJSON(apiAuthorizerToken, &queryParam, data)
- if e2 != nil {
- return nil, e2
- }
-
- result := AuthorizerTokenResult{}
- if err := json.Unmarshal(resp, &result); err != nil {
- return nil, err
- }
-
- return &result, nil
- }
|