1 Star 1 Fork 1

xiaoyutab / xgotool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
get-access-token.go 1.70 KB
一键复制 编辑 原始数据 按行查看 历史
xiaoyutab 提交于 2024-04-30 10:16 . 调整目录结构和依赖位置
package xfaceprint
import (
"fmt"
"time"
"gitee.com/xiaoyutab/xgotool/https"
"gitee.com/xiaoyutab/xgotool/individual/xcache"
"gitee.com/xiaoyutab/xgotool/individual/xlog"
)
// 获取百度AI中心的token标识
func GetAccessToken() string {
cache_key := xcache.Key("sdk.xface.print.baidu.get.access.token")
if xcache.Exists(cache_key) {
return xcache.GetString(cache_key)
}
type request_token struct {
AccessToken string `json:"access_token"` // 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
ExpiresIn int `json:"expires_in"` // 必填,若三方返回的此值json对应错误,请手动重赋值给此值
RefreshToken string `json:"refresh_token"` // 用户刷新access_token
Scope string `json:"scope"` // 用户授权的作用域,使用逗号(,)分隔
Error string `json:"error"` // 错误码
ErrorDescription string `json:"error_description"` // 错误描述信息
}
temp := request_token{}
err := https.New(_default.Domain + "oauth/2.0/token").Param(map[string]string{
"grant_type": "client_credentials",
"client_id": _default.AppKey,
"client_secret": _default.SecretKey,
}).Get().Json(&temp)
if err != nil {
xlog.Panic("百度token请求失败", err)
return ""
}
if temp.Error != "" {
xlog.Panic("百度token获取失败", fmt.Errorf("百度鉴权失败代码:%s, 详情请参阅: https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu", temp.Error))
return ""
}
xcache.SetStructExt(cache_key, temp.AccessToken, time.Hour*24*7) // 缓存一周【有效期30天,但为了兼容考虑不进行缓存那么长时间】
return temp.AccessToken
}
Go
1
https://gitee.com/xiaoyutab/xgotool.git
git@gitee.com:xiaoyutab/xgotool.git
xiaoyutab
xgotool
xgotool
v0.3.13

搜索帮助