1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
oauth2.go 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-07-28 16:16 . 还是把 token 放在这里
package access
import (
"encoding/json"
"fmt"
"gitee.com/h79/gothird/fxg/consts"
"gitee.com/h79/gothird/fxg/params"
"gitee.com/h79/gothird/fxg/response"
"gitee.com/h79/gothird/token"
"gitee.com/h79/gothird/token/access"
"gitee.com/h79/goutils/common/data"
"gitee.com/h79/goutils/common/http"
)
// GetAccessToken
// https://openapi-fxg.jinritemai.com/token/create?app_key=your_app_key&method=token.create&param_json={"code":"your_code","grant_type":"authorization_code"}&timestamp=2021-06-07 17:34:46&v=2&sign=your_sign_here
func GetAccessToken(tk token.Token, d data.D) (access.Token, error) {
parent := tk.Parent()
param := data.D{}
param["code"] = d.String("code")
param["grant_type"] = "authorization_code"
pa := params.UrlParams(parent.GetAppId(), parent.GetSecret(), "token.create", param)
url := fmt.Sprintf("%s/token/create?%s", consts.ApiPrefixUrl, pa)
return getToken(url)
}
// RefreshToken
// https://openapi-fxg.jinritemai.com/token/refresh?app_key=your_app_key&method=token.refresh&param_json={"grant_type":"refresh_token","refresh_token":"your_refresh_token"}&timestamp=2021-06-07 17:36:56&v=2&sign=your_sign
func RefreshToken(tk token.Token, d data.D) (access.Token, error) {
parent := tk.Parent()
param := data.D{}
param["refresh_token"] = d.String("refToken")
param["grant_type"] = "refresh_token"
pa := params.UrlParams(parent.GetAppId(), parent.GetSecret(), "token.refresh", param)
url := fmt.Sprintf("%s/token/refresh?%s", consts.ApiPrefixUrl, pa)
return getToken(url)
}
func getToken(uri string) (*AcsData, error) {
hp := http.Http{}
body, err := hp.DoBytes("GET", uri, nil)
if err != nil {
return nil, err
}
res := tokenResult{}
if err = json.Unmarshal(body, &res); err != nil {
return nil, err
}
return &res.D, res.ErrorIf()
}
type tokenResult struct {
response.Response
D AcsData `json:"data"`
}
type AcsData struct {
Token
ShopId int64 `json:"shop_id,omitempty"`
ShopName string `json:"shop_name,omitempty"`
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.8.103

搜索帮助

344bd9b3 5694891 D2dac590 5694891