This commit is contained in:
邹宗楠
2023-03-30 18:08:18 +08:00
parent de5cb4ca5e
commit 980fb17b1d
6 changed files with 104 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package kuaishou_mini
import (
"errors"
"fmt"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
@@ -13,18 +14,19 @@ const (
// KuaiShouBashUrl 基础域名
KuaiShouBashUrl = "https://open.kuaishou.com" // 域名
// 获取授权信息
KuaiShouAuthLogin = KuaiShouBashUrl + "/oauth2/mp/code2session" // 授权登录
KuaiShouAuthLogin = KuaiShouBashUrl + "/oauth2/mp/code2session" // 授权登录
KuaiShouGetToken = KuaiShouBashUrl + "/oauth2/access_token" // 获取授权token
KuaiShouPreCreateOrder = KuaiShouBashUrl + "/openapi/mp/developer/epay/create_order" // 预下单接口
)
type API struct {
appSecret string // 应用唯一标识对应的密钥
appId string // 应用唯一标识
client *http.Client
config *platformapi.APIConfig
locker sync.RWMutex
msgToken string // accessToken
expiresIn int64 // 过期时间
appSecret string // 应用唯一标识对应的密钥
appId string // 应用唯一标识
client *http.Client
config *platformapi.APIConfig
locker sync.RWMutex
accessToken string // accessToken
expiresIn int64 // 过期时间
}
func (a *API) GetAppID() string {
@@ -49,9 +51,32 @@ func New(appSecret, appId string, config ...*platformapi.APIConfig) *API {
}
}
// GetToken 获取快手token
func (a *API) GetToken() error {
if a.appId == "" || a.appSecret == "" {
return errors.New("快手appId为空或appSecret为空")
}
result, err := a.AccessAPI2(KuaiShouGetToken, map[string]interface{}{"app_id": a.appId, "app_secret": a.appSecret, "grant_type": "client_credentials"})
if err != nil {
return err
}
var data *GetAutoTokenRes
if err := utils.Map2StructByJson(result, &data, false); err != nil {
return err
}
if data.Result != 1 {
return errors.New(utils.Int64ToStr(data.Result))
}
a.expiresIn = data.ExpiresIn
a.accessToken = data.AccessToken
return nil
}
// AccessAPI2 发送请求
func (a *API) AccessAPI2(url string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
request, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(utils.Map2URLValues(params).Encode()))