1
This commit is contained in:
@@ -1,73 +1,74 @@
|
|||||||
package tiktok
|
package tiktok
|
||||||
|
|
||||||
import (
|
//
|
||||||
"encoding/json"
|
//import (
|
||||||
"fmt"
|
// "encoding/json"
|
||||||
"git.rosy.net.cn/baseapi/platformapi"
|
// "fmt"
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
// "git.rosy.net.cn/baseapi/platformapi"
|
||||||
"net/http"
|
// "git.rosy.net.cn/baseapi/utils"
|
||||||
"strings"
|
// "net/http"
|
||||||
)
|
// "strings"
|
||||||
|
//)
|
||||||
func (a *API) GetAppID() string {
|
//
|
||||||
return a.clientKey
|
//func (a *API) GetAppID() string {
|
||||||
}
|
// return a.clientKey
|
||||||
|
//}
|
||||||
func (a *API) GetSecret() string {
|
//
|
||||||
return a.clientSecret
|
//func (a *API) GetSecret() string {
|
||||||
}
|
// return a.clientSecret
|
||||||
|
//}
|
||||||
func New(clientSecret, clientKey string, config ...*platformapi.APIConfig) *API {
|
//
|
||||||
curConfig := platformapi.DefAPIConfig
|
//func New(clientSecret, clientKey string, config ...*platformapi.APIConfig) *API {
|
||||||
if len(config) > 0 {
|
// curConfig := platformapi.DefAPIConfig
|
||||||
curConfig = *config[0]
|
// if len(config) > 0 {
|
||||||
}
|
// curConfig = *config[0]
|
||||||
return &API{
|
// }
|
||||||
clientSecret: clientSecret,
|
// return &API{
|
||||||
clientKey: clientKey,
|
// clientSecret: clientSecret,
|
||||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
// clientKey: clientKey,
|
||||||
config: &curConfig,
|
// client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||||
}
|
// config: &curConfig,
|
||||||
}
|
// }
|
||||||
|
//}
|
||||||
func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
//
|
||||||
// 序列化
|
//func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||||
data, err := json.Marshal(bizParams)
|
// // 序列化
|
||||||
if err != nil {
|
// data, err := json.Marshal(bizParams)
|
||||||
return nil, err
|
// if err != nil {
|
||||||
}
|
// return nil, err
|
||||||
|
// }
|
||||||
// 全路径请求参数
|
//
|
||||||
fullURL := utils.GenerateGetURL(baseUrl, actionApi, nil)
|
// // 全路径请求参数
|
||||||
|
// fullURL := utils.GenerateGetURL(baseUrl, actionApi, nil)
|
||||||
// 发送请求
|
//
|
||||||
sendUrl := func() *http.Request {
|
// // 发送请求
|
||||||
var request *http.Request
|
// sendUrl := func() *http.Request {
|
||||||
if http.MethodPost == method {
|
// var request *http.Request
|
||||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
// if http.MethodPost == method {
|
||||||
} else {
|
// request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
||||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil)
|
// } else {
|
||||||
}
|
// request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil)
|
||||||
switch actionApi {
|
// }
|
||||||
case TiktokTokenUrl:
|
// switch actionApi {
|
||||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
// case TiktokTokenUrl:
|
||||||
case TiktokRefreshTokenUrl:
|
// request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
request.Header.Set("Content-Type", "multipart/form-data")
|
// case TiktokRefreshTokenUrl:
|
||||||
}
|
// request.Header.Set("Content-Type", "multipart/form-data")
|
||||||
|
// }
|
||||||
return request
|
//
|
||||||
}
|
// return request
|
||||||
|
// }
|
||||||
// 数据解析
|
//
|
||||||
dataMarshal := func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
// // 数据解析
|
||||||
if jsonResult1 == nil {
|
// dataMarshal := func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
// if jsonResult1 == nil {
|
||||||
}
|
// return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||||
|
// }
|
||||||
retVal = jsonResult1
|
//
|
||||||
return errLevel, err
|
// retVal = jsonResult1
|
||||||
}
|
// return errLevel, err
|
||||||
|
// }
|
||||||
err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal)
|
//
|
||||||
return retVal, err
|
// err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal)
|
||||||
}
|
// return retVal, err
|
||||||
|
//}
|
||||||
|
|||||||
@@ -1,24 +1,25 @@
|
|||||||
package tiktok
|
package tiktok
|
||||||
|
|
||||||
import (
|
//
|
||||||
"git.rosy.net.cn/baseapi/platformapi"
|
//import (
|
||||||
"net/http"
|
// "git.rosy.net.cn/baseapi/platformapi"
|
||||||
"sync"
|
// "net/http"
|
||||||
)
|
// "sync"
|
||||||
|
//)
|
||||||
const (
|
//
|
||||||
BaseURL = "https://open.douyin.com"
|
//const (
|
||||||
TiktokTokenUrl = "/oauth/access_token/"
|
// BaseURL = "https://open.douyin.com"
|
||||||
TiktokRefreshTokenUrl = "/oauth/renew_refresh_token/"
|
// TiktokTokenUrl = "/oauth/access_token/"
|
||||||
)
|
// TiktokRefreshTokenUrl = "/oauth/renew_refresh_token/"
|
||||||
|
//)
|
||||||
type API struct {
|
//
|
||||||
clientSecret string // 应用唯一标识对应的密钥
|
//type API struct {
|
||||||
clientKey string // 应用唯一标识
|
// clientSecret string // 应用唯一标识对应的密钥
|
||||||
client *http.Client
|
// clientKey string // 应用唯一标识
|
||||||
config *platformapi.APIConfig
|
// client *http.Client
|
||||||
locker sync.RWMutex
|
// config *platformapi.APIConfig
|
||||||
|
// locker sync.RWMutex
|
||||||
msgToken string
|
//
|
||||||
msgKey string
|
// msgToken string
|
||||||
}
|
// msgKey string
|
||||||
|
//}
|
||||||
|
|||||||
@@ -1,81 +1,82 @@
|
|||||||
package tiktok
|
package tiktok
|
||||||
|
|
||||||
import (
|
//
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
//import (
|
||||||
"net/http"
|
// "git.rosy.net.cn/baseapi/utils"
|
||||||
)
|
// "net/http"
|
||||||
|
//)
|
||||||
// OauthAccessTokenResData access_token
|
//
|
||||||
type OauthAccessTokenResData struct {
|
//// OauthAccessTokenResData access_token
|
||||||
AccessToken string `json:"access_token"` // 接口调用凭证
|
//type OauthAccessTokenResData struct {
|
||||||
UnionId string `json:"union_id"` // 当且仅当该网站应用已获得该用户的userinfo授权时,才会出现该字段。
|
// AccessToken string `json:"access_token"` // 接口调用凭证
|
||||||
Scope string `json:"scope"` // 用户授权的作用域(Scope),使用逗号(,)分隔,开放平台几乎几乎每个接口都需要特定的Scope。
|
// UnionId string `json:"union_id"` // 当且仅当该网站应用已获得该用户的userinfo授权时,才会出现该字段。
|
||||||
ExpiresIn uint64 `json:"expires_in"` // access_token接口调用凭证超时时间,单位(秒)
|
// Scope string `json:"scope"` // 用户授权的作用域(Scope),使用逗号(,)分隔,开放平台几乎几乎每个接口都需要特定的Scope。
|
||||||
OpenId string `json:"open_id"` // 授权用户唯一标识
|
// ExpiresIn uint64 `json:"expires_in"` // access_token接口调用凭证超时时间,单位(秒)
|
||||||
RefreshToken string `json:"refresh_token"` // 用户刷新access_token
|
// OpenId string `json:"open_id"` // 授权用户唯一标识
|
||||||
DYError
|
// RefreshToken string `json:"refresh_token"` // 用户刷新access_token
|
||||||
}
|
// DYError
|
||||||
|
//}
|
||||||
// DYError 错误结构体
|
//
|
||||||
type DYError struct {
|
//// DYError 错误结构体
|
||||||
ErrorCode int64 `json:"error_code,omitempty"` // 错误码
|
//type DYError struct {
|
||||||
Description string `json:"description,omitempty"` // 错误码描述
|
// ErrorCode int64 `json:"error_code,omitempty"` // 错误码
|
||||||
}
|
// Description string `json:"description,omitempty"` // 错误码描述
|
||||||
|
//}
|
||||||
// OauthAccessTokenRes access_token
|
//
|
||||||
type OauthAccessTokenRes struct {
|
//// OauthAccessTokenRes access_token
|
||||||
Data OauthAccessTokenResData `json:"data"`
|
//type OauthAccessTokenRes struct {
|
||||||
Message string `json:"message"`
|
// Data OauthAccessTokenResData `json:"data"`
|
||||||
}
|
// Message string `json:"message"`
|
||||||
|
//}
|
||||||
// 获取抖音token
|
//
|
||||||
func (a *API) GetTiktokToken(code string) (*OauthAccessTokenRes, error) {
|
//// 获取抖音token
|
||||||
tokenReq := make(map[string]interface{}, 4)
|
//func (a *API) GetTiktokToken(code string) (*OauthAccessTokenRes, error) {
|
||||||
tokenReq["client_secret"] = a.clientSecret
|
// tokenReq := make(map[string]interface{}, 4)
|
||||||
tokenReq["code"] = code
|
// tokenReq["client_secret"] = a.clientSecret
|
||||||
tokenReq["grant_type"] = "authorization_code"
|
// tokenReq["code"] = code
|
||||||
tokenReq["client_key"] = a.clientKey
|
// tokenReq["grant_type"] = "authorization_code"
|
||||||
result, err := a.AccessAPI(BaseURL, TiktokTokenUrl, http.MethodPost, tokenReq)
|
// tokenReq["client_key"] = a.clientKey
|
||||||
if err != nil {
|
// result, err := a.AccessAPI(BaseURL, TiktokTokenUrl, http.MethodPost, tokenReq)
|
||||||
return nil, err
|
// if err != nil {
|
||||||
}
|
// return nil, err
|
||||||
|
// }
|
||||||
oauthAccessToken := &OauthAccessTokenRes{}
|
//
|
||||||
if err := utils.Map2StructByJson(result, oauthAccessToken, false); err != nil {
|
// oauthAccessToken := &OauthAccessTokenRes{}
|
||||||
return nil, err
|
// if err := utils.Map2StructByJson(result, oauthAccessToken, false); err != nil {
|
||||||
}
|
// return nil, err
|
||||||
return oauthAccessToken, nil
|
// }
|
||||||
}
|
// return oauthAccessToken, nil
|
||||||
|
//}
|
||||||
// OauthRefreshTokenResData 刷新access_token
|
//
|
||||||
type OauthRefreshTokenResData struct {
|
//// OauthRefreshTokenResData 刷新access_token
|
||||||
AccessToken string `json:"access_token"` // 接口调用凭证
|
//type OauthRefreshTokenResData struct {
|
||||||
Scope string `json:"scope"` // 用户授权的作用域
|
// AccessToken string `json:"access_token"` // 接口调用凭证
|
||||||
ExpiresIn uint64 `json:"expires_in"` // 过期时间,单位(秒)
|
// Scope string `json:"scope"` // 用户授权的作用域
|
||||||
OpenId string `json:"open_id"` // 当前应用下,授权用户唯一标识
|
// ExpiresIn uint64 `json:"expires_in"` // 过期时间,单位(秒)
|
||||||
RefreshToken string `json:"refresh_token"` // 用户刷新
|
// OpenId string `json:"open_id"` // 当前应用下,授权用户唯一标识
|
||||||
DYError
|
// RefreshToken string `json:"refresh_token"` // 用户刷新
|
||||||
}
|
// DYError
|
||||||
|
//}
|
||||||
// OauthRefreshTokenRes 刷新access_token
|
//
|
||||||
type OauthRefreshTokenRes struct {
|
//// OauthRefreshTokenRes 刷新access_token
|
||||||
Data OauthRefreshTokenResData `json:"data"`
|
//type OauthRefreshTokenRes struct {
|
||||||
Message string `json:"message"`
|
// Data OauthRefreshTokenResData `json:"data"`
|
||||||
}
|
// Message string `json:"message"`
|
||||||
|
//}
|
||||||
// 刷新用户Refresh token
|
//
|
||||||
func (a *API) RefreshToken(refreshToken string) (*OauthRefreshTokenRes, error) {
|
//// 刷新用户Refresh token
|
||||||
tokenReq := make(map[string]interface{}, 2)
|
//func (a *API) RefreshToken(refreshToken string) (*OauthRefreshTokenRes, error) {
|
||||||
tokenReq["client_key"] = a.clientKey
|
// tokenReq := make(map[string]interface{}, 2)
|
||||||
tokenReq["refresh_token"] = refreshToken
|
// tokenReq["client_key"] = a.clientKey
|
||||||
// 通过旧的refresh_token获取新的refresh_token,调用后旧refresh_token会失效,新refresh_token有30天有效期。最多只能获取5次新的refresh_token,5次过后需要用户重新授权。
|
// tokenReq["refresh_token"] = refreshToken
|
||||||
result, err := a.AccessAPI(BaseURL, TiktokRefreshTokenUrl, http.MethodPost, tokenReq)
|
// // 通过旧的refresh_token获取新的refresh_token,调用后旧refresh_token会失效,新refresh_token有30天有效期。最多只能获取5次新的refresh_token,5次过后需要用户重新授权。
|
||||||
if err != nil {
|
// result, err := a.AccessAPI(BaseURL, TiktokRefreshTokenUrl, http.MethodPost, tokenReq)
|
||||||
return nil, err
|
// if err != nil {
|
||||||
}
|
// return nil, err
|
||||||
oauthRefreshToken := &OauthRefreshTokenRes{}
|
// }
|
||||||
if err := utils.Map2StructByJson(result, oauthRefreshToken, false); err != nil {
|
// oauthRefreshToken := &OauthRefreshTokenRes{}
|
||||||
return nil, err
|
// if err := utils.Map2StructByJson(result, oauthRefreshToken, false); err != nil {
|
||||||
}
|
// return nil, err
|
||||||
return oauthRefreshToken, nil
|
// }
|
||||||
}
|
// return oauthRefreshToken, nil
|
||||||
|
//}
|
||||||
|
|||||||
Reference in New Issue
Block a user