1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
token.go 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-09-17 19:33 . 基于服务商企业微信 1.0.1
package weixin
import (
"encoding/json"
"fmt"
"gitee.com/h79/gothird/token"
access2 "gitee.com/h79/gothird/token/access"
"gitee.com/h79/gothird/weixin/access"
"gitee.com/h79/gothird/weixin/consts"
"gitee.com/h79/gothird/weixin/response"
"gitee.com/h79/goutils/common/data"
"gitee.com/h79/goutils/common/http"
"gitee.com/h79/goutils/common/result"
)
// 1 公众号: 全局唯一接口调用凭据
// 2 小程序: 全局唯一后台接口调用凭据(access_token)。调用绝大多数后台接口时都需使用 access_token
// 这种没有刷新refreshToken, 只有到期时,再调用一次,取一个新的access token
func getTokenUrl(token token.Token) string {
return fmt.Sprintf("%s/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", consts.ApiPrefixUrl, token.GetAppId(), token.GetSecret())
}
func GetAccessToken(tk token.Token, d data.D) (access2.Token, error) {
hp := http.Http{}
body, err := hp.DoBytes("GET", getTokenUrl(tk), nil)
if err != nil {
return nil, err
}
type tokenResult struct {
response.Response
access.Token
}
res := tokenResult{}
if err = json.Unmarshal(body, &res); err != nil {
return nil, err
}
if res.ErrCode != 0 {
return nil, result.Error(res.ErrCode, res.ErrMsg)
}
return &res.Token, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.0.56

搜索帮助

344bd9b3 5694891 D2dac590 5694891