1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
token.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-07-25 17:58 . 把token放到 goutils
package weixin
import (
"encoding/json"
"fmt"
"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/token"
tokenaccess "gitee.com/h79/goutils/token/access"
)
// 1 公众号: 全局唯一接口调用凭据
// 2 小程序: 全局唯一后台接口调用凭据(access_token)。调用绝大多数后台接口时都需使用 access_token
// 这种没有刷新refreshToken, 只有到期时,再调用一次,取一个新的access token
func getTokenUrl(appId, secret string) string {
return fmt.Sprintf("%s/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", consts.ApiPrefixUrl, appId, secret)
}
func GetAccessToken(tk token.Token, d data.D) (tokenaccess.Token, error) {
var appId = tk.GetAppId()
var secret = tk.GetSecret()
if appId == "" {
return nil, fmt.Errorf("app id is empty")
}
if secret == "" {
return nil, fmt.Errorf("secret is empty")
}
var hp = http.Http{}
var body, err = hp.DoBytes("GET", getTokenUrl(appId, secret), 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
}
return &res.Token, res.ErrorIf()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.8.4

搜索帮助

344bd9b3 5694891 D2dac590 5694891