1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
oauth2.go 1.99 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-05-14 18:13 . 公众号
package oauth2
import (
"fmt"
"gitee.com/h79/gothird/token"
"gitee.com/h79/gothird/token/access"
"gitee.com/h79/gothird/weixin/consts"
"gitee.com/h79/gothird/weixin/session"
"gitee.com/h79/goutils/common/data"
)
// 针对微信公众号,个人用户
// 首先请注意,这里通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。
// 公众号可通过下述接口来获取网页授权access_token。
// 如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。
// 尤其注意:由于公众号的secret和获取到的access_token安全级别都非常高,必须只保存在服务器,不允许传给客户端。
// 后续刷新access_token、通过access_token获取用户信息等步骤,也必须从服务器发起。
// GetAccessToken 官司说明 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
// 微信网页通过code 授权access_token, 这个授权只是某一个具体的app,某个具体openid对应的access_token
func GetAccessToken(tk token.Token, d data.D) (access.Token, error) {
p := tk.Parent()
code := d.String("code")
url := fmt.Sprintf("%s/sns/oauth2/access_token?appid=%s&secret=%s&grant_type=authorization_code&code=%s", consts.ApiPrefixUrl, p.GetAppId(), p.GetSecret(), code)
se, err := session.GetToken(p.GetAppId(), url)
if err != nil {
return nil, err
}
return se, nil
}
// RefreshToken 刷新access_token
// https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=AppID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
func RefreshToken(tk token.Token, d data.D) (access.Token, error) {
p := tk.Parent()
refToken := d.String("refToken")
url := fmt.Sprintf("%s/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s", consts.ApiPrefixUrl, p.GetAppId(), refToken)
return session.GetToken(p.GetAppId(), url)
}
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.8.103

搜索帮助