添加国美api,删除老版本蜂鸟API,添加抖音授权api、

This commit is contained in:
邹宗楠
2022-05-13 16:03:37 +08:00
parent 56ef57fae5
commit ff5b116ce5
35 changed files with 2346 additions and 1191 deletions

View File

@@ -1,19 +1,20 @@
package tiktok
import (
"errors"
"git.rosy.net.cn/baseapi/utils"
"net/http"
)
// OauthAccessTokenResData access_token
type OauthAccessTokenResData struct {
AccessToken string `json:"access_token"` // 接口调用凭证
UnionId string `json:"union_id"` // 当且仅当该网站应用已获得该用户的userinfo授权时才会出现该字段。
Scope string `json:"scope"` // 用户授权的作用域(Scope),使用逗号(,分隔开放平台几乎几乎每个接口都需要特定的Scope。
ExpiresIn uint64 `json:"expires_in"` // access_token接口调用凭证超时时间,单位(秒
OpenId string `json:"open_id"` // 授权用户唯一标识
RefreshToken string `json:"refresh_token"` // 用户刷新access_token
DYError
ErrorCode int64 `json:"error_code"` // 错误码
ExpiresIn uint64 `json:"expires_in"` // access_token接口调用凭证超时时间单位
OpenId string `json:"open_id"` // 授权用户唯一标识
RefreshExpiresIn int64 `json:"refresh_expires_in"` // refresh_token凭证超时时间单位)
RefreshToken string `json:"refresh_token"` // 用户刷新access_token
Scope string `json:"scope"` // 用户授权的作用域(Scope),使用逗号(,分隔开放平台几乎几乎每个接口都需要特定的Scope。
AccessToken string `json:"access_token"` // 接口调用凭证
Description string `json:"description"` // 错误码描述
}
// DYError 错误结构体
@@ -24,8 +25,21 @@ type DYError struct {
// OauthAccessTokenRes access_token
type OauthAccessTokenRes struct {
Data OauthAccessTokenResData `json:"data"`
Message string `json:"message"`
Data *OauthAccessTokenResData
Message string `json:"message"`
}
// 抖音登录返回值
type TiktokOauthResone struct {
ErrNo int64 `json:"err_no"`
ErrTips string `json:"err_tips"`
Data *struct {
SessionKey string `json:"session_key"`
OpenId string `json:"openid"` // 授权用户唯一标识
AnonymousOpenid string `json:"anonymous_openid"`
Unionid string `json:"unionid"`
Dopenid string `json:"dopenid"`
}
}
// 获取抖音token
@@ -35,7 +49,8 @@ func (a *API) GetTiktokToken(code string) (*OauthAccessTokenRes, error) {
tokenReq["code"] = code
tokenReq["grant_type"] = "authorization_code"
tokenReq["client_key"] = a.clientKey
result, err := a.AccessAPI(BaseURL, TiktokTokenUrl, http.MethodPost, tokenReq)
result, err := a.AccessAPI2(BaseURL, tokenReq)
if err != nil {
return nil, err
}
@@ -44,6 +59,28 @@ func (a *API) GetTiktokToken(code string) (*OauthAccessTokenRes, error) {
if err := utils.Map2StructByJson(result, oauthAccessToken, false); err != nil {
return nil, err
}
if oauthAccessToken.Data.ErrorCode != 0 {
return nil, errors.New(oauthAccessToken.Data.Description)
}
return oauthAccessToken, nil
}
// 获取抖音登录授权2
func (a *API) GetTiktokToken2(code string) (*TiktokOauthResone, error) {
tokenReq := make(map[string]interface{}, 3)
tokenReq["appid"] = a.GetAppID()
tokenReq["code"] = code
tokenReq["secret"] = a.GetSecret()
tokenReq["anonymous_code"] = ""
result, err := a.AccessAPI2("https://developer.toutiao.com"+"/"+"api/apps/v2/jscode2session", tokenReq)
if err != nil {
return nil, err
}
oauthAccessToken := &TiktokOauthResone{}
if err := utils.Map2StructByJson(result, oauthAccessToken, false); err != nil {
return nil, err
}
return oauthAccessToken, nil
}
@@ -69,7 +106,7 @@ func (a *API) RefreshToken(refreshToken string) (*OauthRefreshTokenRes, error) {
tokenReq["client_key"] = a.clientKey
tokenReq["refresh_token"] = refreshToken
// 通过旧的refresh_token获取新的refresh_token调用后旧refresh_token会失效新refresh_token有30天有效期。最多只能获取5次新的refresh_token5次过后需要用户重新授权。
result, err := a.AccessAPI(BaseURL, TiktokRefreshTokenUrl, http.MethodPost, tokenReq)
result, err := a.AccessAPI2(BaseURL, tokenReq)
if err != nil {
return nil, err
}