Score
0
Watch 30 Star 81 Fork 19

北京小程科技有限公司 / 微信Go SDKGoMIT

Join us
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
微信支付、公众号、小程序相关后端接口的Go语言实现。 spread retract

Clone or download
service_auth_api.go 2.02 KB
Copy Edit Web IDE Raw Blame History
leexsoft authored 2019-10-18 20:09 . 修改字段类型
/*
网页授权相关的接口。
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
*/
package wechat
import (
"encoding/json"
"fmt"
)
// 获取网页授权的access_token
func GetAuthAccessToken(appId, appSecret, code string) (accessToken AccessToken, err error) {
url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appId, appSecret, code)
body, err := httpGet(url)
if err != nil {
return
}
err = json.Unmarshal(body, &accessToken)
return
}
// 刷新网页授权的access_token
func RefreshAuthAccessToken(appId, refreshToken string) (accessToken AccessToken, err error) {
url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s", appId, refreshToken)
body, err := httpGet(url)
if err != nil {
return
}
err = json.Unmarshal(body, &accessToken)
return
}
// 获取用户基本信息(授权机制)
func GetAuthUserInfo(accessToken, openId, lang string) (userInfo UserInfo, err error) {
if len(lang) <= 0 {
lang = "zh_CN"
}
url := fmt.Sprintf("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=%s", accessToken, openId, lang)
body, err := httpGet(url)
if err != nil {
return
}
err = json.Unmarshal(body, &userInfo)
return
}
// 检验网页授权的access_token是否有效
func CheckAuthAccessToken(accessToken, openId string) (resp ResponseBase, err error) {
url := fmt.Sprintf("https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s", accessToken, openId)
body, err := httpGet(url)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
// 登录凭证校验
func Code2Session(appId, secret, js_code string) (resp SessionKey, err error) {
url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, secret, js_code)
body, err := httpGet(url)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}

Comment ( 0 )

Sign in for post a comment

Go
1
https://gitee.com/cuckoopark/wechat.git
git@gitee.com:cuckoopark/wechat.git
cuckoopark
wechat
微信Go SDK
master

Help Search