2025-01-13 22:58:50 +08:00
|
|
|
package douyin_openapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
accessToken "gitea.youtukeji.com.cn/xiabin/douyin-openapi/access-token"
|
|
|
|
"gitea.youtukeji.com.cn/xiabin/douyin-openapi/cache"
|
|
|
|
"testing"
|
2025-01-14 18:35:29 +08:00
|
|
|
"time"
|
2025-01-13 22:58:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 声明测试常量
|
|
|
|
const (
|
2025-01-14 18:35:29 +08:00
|
|
|
AppId = "tt8b32fd8f14071db707"
|
|
|
|
AppSecret = "44018e80b1bde34395a52de67ce1e0c37c572d80"
|
2025-01-13 22:58:50 +08:00
|
|
|
Token = ""
|
|
|
|
Salt = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
// 声明一个缓存实例
|
|
|
|
var Cache cache.Cache
|
|
|
|
|
|
|
|
// 声明全局openApi实例
|
|
|
|
var OpenApi *DouYinOpenApi
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Cache = cache.NewMemory()
|
|
|
|
OpenApi = NewDouYinOpenApi(DouYinOpenApiConfig{
|
|
|
|
AppId: AppId,
|
|
|
|
AppSecret: AppSecret,
|
|
|
|
IsSandbox: false,
|
|
|
|
Token: Token,
|
|
|
|
Salt: Salt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 测试获取新的token
|
|
|
|
func TestDouyinOpenapi_NewDefaultAccessToken(t *testing.T) {
|
|
|
|
token := accessToken.NewDefaultAccessToken(AppId, AppSecret, Cache, true)
|
|
|
|
getAccessToken, err := token.GetAccessToken()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got a error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("got a value: %s", getAccessToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 基准测试看获取token的次数?
|
|
|
|
func BenchmarkDouyinOpenapi_NewDefaultAccessToken(b *testing.B) {
|
|
|
|
token := accessToken.NewDefaultAccessToken(AppId, AppSecret, Cache, true)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
getAccessToken, err := token.GetAccessToken()
|
|
|
|
b.Logf("get token: %s %+v", getAccessToken, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 测试小程序登录
|
|
|
|
func TestDouYinOpenApi_Code2Session(t *testing.T) {
|
|
|
|
session, err := OpenApi.Code2Session("1111", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got a error %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("got a value %+v", session)
|
|
|
|
}
|
2025-01-14 18:35:29 +08:00
|
|
|
|
|
|
|
func TestDouYinOpenApi_GetEcpm(t *testing.T) {
|
|
|
|
|
|
|
|
// 获取动态的 access_token
|
|
|
|
token := accessToken.NewDefaultAccessToken(AppId, AppSecret, Cache, true)
|
|
|
|
|
|
|
|
at, err := token.GetAccessToken()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got a error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := OpenApi.GetEcpm(GetEcpmParams{
|
|
|
|
AppId: AppId,
|
|
|
|
OpenId: "_000aG3MQTzVW-yCUH-ndwv90JIyvdTSD0gf",
|
|
|
|
AccessToken: at,
|
|
|
|
DateHour: time.Now().Format(time.DateOnly),
|
|
|
|
PageSize: 500,
|
|
|
|
PageNo: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got a error %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("got a value %+v", list)
|
|
|
|
}
|