保存服务器代码

This commit is contained in:
DESKTOP-DDTRVOR\asus 2025-01-08 15:52:20 +08:00
parent 3e4e569f46
commit 9c94786d7c
13 changed files with 551 additions and 0 deletions

BIN
sever/bf/server.exe Normal file

Binary file not shown.

41
sever/bf/server.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"fmt"
"net/http"
)
func main() {
// 设置路由和处理函数
http.HandleFunc("/get-value", func(w http.ResponseWriter, r *http.Request) {
// 检查请求方法是否为GET
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// 从请求参数中获取"key"的值
key := r.URL.Query().Get("key")
if key == "" {
http.Error(w, "Missing 'key' parameter", http.StatusBadRequest)
return
}
// 根据key返回相应的值这里可以替换为实际的业务逻辑
value := fmt.Sprintf("Value for key '%s'", key)
// 设置响应头
w.Header().Set("Content-Type", "text/plain")
// 返回响应值
w.WriteHeader(http.StatusOK)
w.Write([]byte(value))
})
// 启动HTTP服务器
fmt.Println("Server is running on http://localhost:8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}

BIN
sever/bf/server2.exe Normal file

Binary file not shown.

72
sever/bf/server2.go Normal file
View File

@ -0,0 +1,72 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// 设置路由和处理函数
http.HandleFunc("/get-ecpm", func(w http.ResponseWriter, r *http.Request) {
// 检查请求方法是否为GET
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// 从请求参数中获取所需的值
openID := r.URL.Query().Get("open_id")
mpID := r.URL.Query().Get("mp_id")
dateHour := r.URL.Query().Get("date_hour")
accessToken := r.URL.Query().Get("access_token")
if openID == "" || mpID == "" || dateHour == "" || accessToken == "" {
http.Error(w, "Missing required parameters", http.StatusBadRequest)
return
}
// 构建请求体
requestBody := map[string]string{
"open_id": openID,
"mp_id": mpID,
"date_hour": dateHour,
"access_token": accessToken,
}
requestBodyJSON, err := json.Marshal(requestBody)
if err != nil {
http.Error(w, "Failed to encode request body", http.StatusInternalServerError)
return
}
// 发送请求到抖音API
apiURL := "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm"
resp, err := http.Post(apiURL, "application/json", bytes.NewBuffer(requestBodyJSON))
if err != nil {
http.Error(w, "Failed to call Douyin API", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// 读取响应内容
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, "Failed to read Douyin API response", http.StatusInternalServerError)
return
}
// 将API响应返回给客户端
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
w.Write(responseBody)
})
// 启动HTTP服务器
fmt.Println("Server is running on http://localhost:8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}

BIN
sever/bf/test.exe Normal file

Binary file not shown.

BIN
sever/server.exe Normal file

Binary file not shown.

41
sever/server.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"fmt"
"net/http"
)
func main() {
// 设置路由和处理函数
http.HandleFunc("/get-value", func(w http.ResponseWriter, r *http.Request) {
// 检查请求方法是否为GET
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// 从请求参数中获取"key"的值
key := r.URL.Query().Get("key")
if key == "" {
http.Error(w, "Missing 'key' parameter", http.StatusBadRequest)
return
}
// 根据key返回相应的值这里可以替换为实际的业务逻辑
value := fmt.Sprintf("Value for key '%s'", key)
// 设置响应头
w.Header().Set("Content-Type", "text/plain")
// 返回响应值
w.WriteHeader(http.StatusOK)
w.Write([]byte(value))
})
// 启动HTTP服务器
fmt.Println("Server is running on http://localhost:8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}

BIN
sever/server2.exe Normal file

Binary file not shown.

109
sever/server2.go Normal file
View File

@ -0,0 +1,109 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"fmt"
"testing"
credential "github.com/bytedance/douyin-openapi-credential-go/client"
openApiSdkClient "github.com/bytedance/douyin-openapi-sdk-go/client"
)
func main() {
// 设置路由和处理函数
http.HandleFunc("/get-ecpm", func(w http.ResponseWriter, r *http.Request) {
// 检查请求方法是否为GET
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// 从请求参数中获取所需的值
openID := r.URL.Query().Get("open_id")
mpID := r.URL.Query().Get("mp_id")
dateHour := r.URL.Query().Get("date_hour")
if openID == "" || mpID == "" || dateHour == "" {
http.Error(w, "Missing required parameters", http.StatusBadRequest)
return
}
opt := new(credential.Config).SetClientKey("<your_app_id>").SetClientSecret("<your_secret>")
sdkClient, err := openApiSdkClient.NewClient(opt)
if err != nil {
t.Log("sdk init err:", err)
return
}
appId := "tt4233**"
grantType := "4Rd5eQpX8T"
secret := "z44Y1MZMPc"
response, err := getAppsV2Token(sdkClient, appId, grantType, secret)
if err != nil {
t.Error(err)
} else {
t.Log(response) // 或者处理你需要的response内容
}
// 构建请求体
requestBody := map[string]string{
"open_id": openID,
"mp_id": mpID,
"date_hour": dateHour,
"access_token": response,
}
requestBodyJSON, err := json.Marshal(requestBody)
if err != nil {
http.Error(w, "Failed to encode request body", http.StatusInternalServerError)
return
}
// 发送请求到抖音API
apiURL := "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm"
resp, err := http.Post(apiURL, "application/json", bytes.NewBuffer(requestBodyJSON))
if err != nil {
http.Error(w, "Failed to call Douyin API", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// 读取响应内容
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, "Failed to read Douyin API response", http.StatusInternalServerError)
return
}
// 将API响应返回给客户端
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
w.Write(responseBody)
})
// 启动HTTP服务器
fmt.Println("Server is running on http://localhost:8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}
func getAppsV2Token(sdkClient *openApiSdkClient.Client, appId, grantType, secret string) (*openApiSdkClient.AppsV2TokenResponse, error) {
sdkRequest := &openApiSdkClient.AppsV2TokenRequest{
Appid: appId,
GrantType: grantType,
Secret: secret,
}
// 发送请求并获取响应
var response *openApiSdkClient.AppsV2TokenResponse
err := sdkClient.AppsV2Token(sdkRequest, &response)
if err != nil {
return nil, fmt.Errorf("failed to retrieve Apps V2 token: %v", err)
}
return response, nil
}

19
sever/test/go.mod Normal file
View File

@ -0,0 +1,19 @@
module test
go 1.23.4
require (
github.com/bytedance/douyin-openapi-credential-go v0.0.0-20240627133153-7f4587ca06ce
github.com/bytedance/douyin-openapi-sdk-go v0.0.0-20240925072830-12f094544623
)
require (
github.com/alibabacloud-go/debug v1.0.1 // indirect
github.com/alibabacloud-go/tea v1.2.2 // indirect
github.com/bytedance/douyin-openapi-util-go v0.0.0-20240627134255-db766d8741c8 // indirect
github.com/go-resty/resty/v2 v2.16.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/net v0.34.0 // indirect
)

82
sever/test/go.sum Normal file
View File

@ -0,0 +1,82 @@
github.com/alibabacloud-go/debug v1.0.0/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc=
github.com/alibabacloud-go/debug v1.0.1 h1:MsW9SmUtbb1Fnt3ieC6NNZi6aEwrXfDksD4QA6GSbPg=
github.com/alibabacloud-go/debug v1.0.1/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc=
github.com/alibabacloud-go/tea v1.2.2 h1:aTsR6Rl3ANWPfqeQugPglfurloyBJY85eFy7Gc1+8oU=
github.com/alibabacloud-go/tea v1.2.2/go.mod h1:CF3vOzEMAG+bR4WOql8gc2G9H3EkH3ZLAQdpmpXMgwk=
github.com/bytedance/douyin-openapi-credential-go v0.0.0-20240627133153-7f4587ca06ce h1:Mn9pJpYYR5tNQZEUorCj2NZoBWbCDjRZTbgRB4hMQXI=
github.com/bytedance/douyin-openapi-credential-go v0.0.0-20240627133153-7f4587ca06ce/go.mod h1:OKJKotnRJazXhZzj4dwUvdw9OuupYNJmctz70MoJme8=
github.com/bytedance/douyin-openapi-sdk-go v0.0.0-20240925072830-12f094544623 h1:NxYsIQCpexUPrzMnotXbTiCJKmhH9IsvjOGGOv+iztk=
github.com/bytedance/douyin-openapi-sdk-go v0.0.0-20240925072830-12f094544623/go.mod h1:dsLFkIt2aKodjL5Y+JDS0wK0PWb9/I7RwNu8JAixtPs=
github.com/bytedance/douyin-openapi-util-go v0.0.0-20240627134255-db766d8741c8 h1:71WIUeJE02/oi/sgrIseKSGBQtiA2Dofl/pV7oe4TZk=
github.com/bytedance/douyin-openapi-util-go v0.0.0-20240627134255-db766d8741c8/go.mod h1:GPiogxOAuOSzXMhJ+akYQBLnb6+lGv24kf8JBMtBb2Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-resty/resty/v2 v2.12.0/go.mod h1:o0yGPrkS3lOe1+eFajk6kBW8ScXzwU3hD69/gt2yB/0=
github.com/go-resty/resty/v2 v2.16.2 h1:CpRqTjIzq/rweXUt9+GxzzQdlkqMdt8Lm/fuK/CAbAg=
github.com/go-resty/resty/v2 v2.16.2/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

187
sever/test/server2.go Normal file
View File

@ -0,0 +1,187 @@
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
credential "github.com/bytedance/douyin-openapi-credential-go/client"
openApiSdkClient "github.com/bytedance/douyin-openapi-sdk-go/client"
)
// 获取 access_token 的函数
func GetAccessToken() (string, error) {
// 初始化 SDK 客户端,设置 app_id 和 secret
opt := new(credential.Config).
SetClientKey("tt8b32fd8f14071db707"). // 替换为你的 app_id
SetClientSecret("44018e80b1bde34395a52de67ce1e0c37c572d80") // 替换为你的 secret
// 创建 SDK 客户端
sdkClient, err := openApiSdkClient.NewClient(opt)
if err != nil {
return "", fmt.Errorf("SDK 初始化失败: %v", err)
}
// 构造获取 token 的请求参数
sdkRequest := &openApiSdkClient.AppsV2TokenRequest{}
sdkRequest.SetAppid("tt8b32fd8f14071db707") // 设置应用 ID
sdkRequest.SetGrantType("client_credential") // 设置授权类型client_credentials 模式)
sdkRequest.SetSecret("44018e80b1bde34395a52de67ce1e0c37c572d80") // 设置密钥
// 调用 SDK 获取 access_token
sdkResponse, err := sdkClient.AppsV2Token(sdkRequest)
if err != nil {
fmt.Println("sdk call err:", err)
return "", nil
}
fmt.Println("sdk数据", sdkResponse.Data)
// 返回 access_token
return *sdkResponse.Data.AccessToken, nil
}
func DecryptAES128CBC(encryptedData, sessionKey, iv string) ([]byte, error) {
// Base64 解码 key 和 iv
aesKey, err := base64.StdEncoding.DecodeString(sessionKey)
if err != nil {
return nil, fmt.Errorf("sessionKey Base64 解码失败: %v", err)
}
initialVector, err := base64.StdEncoding.DecodeString(iv)
if err != nil {
return nil, fmt.Errorf("iv Base64 解码失败: %v", err)
}
// 检查 key 和 iv 长度是否为 16 字节
if len(aesKey) != 16 {
return nil, fmt.Errorf("AES key 长度必须为 16 字节")
}
if len(initialVector) != 16 {
return nil, fmt.Errorf("IV 长度必须为 16 字节")
}
// Base64 解码密文
ciphertext, err := base64.StdEncoding.DecodeString(encryptedData)
if err != nil {
return nil, fmt.Errorf("encryptedData Base64 解码失败: %v", err)
}
// 创建 AES 块
block, err := aes.NewCipher(aesKey)
if err != nil {
return nil, fmt.Errorf("创建 AES Cipher 失败: %v", err)
}
// 检查密文长度是否为块大小的倍数
if len(ciphertext)%aes.BlockSize != 0 {
return nil, fmt.Errorf("密文长度必须是 AES 块大小的倍数")
}
// 创建 CBC 解密器
mode := cipher.NewCBCDecrypter(block, initialVector)
// 解密数据
decrypted := make([]byte, len(ciphertext))
mode.CryptBlocks(decrypted, ciphertext)
// 去除 PKCS#7 填充
decrypted, err = PKCS7Unpad(decrypted)
if err != nil {
return nil, fmt.Errorf("去除 PKCS#7 填充失败: %v", err)
}
return decrypted, nil
}
// PKCS#7 填充去除函数
func PKCS7Unpad(data []byte) ([]byte, error) {
length := len(data)
if length == 0 {
return nil, fmt.Errorf("解密后数据长度为 0")
}
padding := int(data[length-1])
if padding > length || padding == 0 {
return nil, fmt.Errorf("无效的填充数据")
}
return data[:length-padding], nil
}
func main() {
// 定义 HTTP 路由和处理函数
http.HandleFunc("/get-ecpm", func(w http.ResponseWriter, r *http.Request) {
// 检查 HTTP 请求方法是否为 GET
if r.Method != http.MethodGet {
http.Error(w, "无效的请求方法", http.StatusMethodNotAllowed)
return
}
// 从请求中获取参数
openID := r.URL.Query().Get("open_id") // 获取 open_id
mpID := r.URL.Query().Get("mp_id") // 获取 mp_id
iv := r.URL.Query().Get("iv") // 获取初始向量
dateHour := r.URL.Query().Get("date_hour") // 获取 date_hour
// 检查是否有缺少参数
if openID == "" || mpID == "" || dateHour == "" {
http.Error(w, "缺少必要的参数", http.StatusBadRequest)
return
}
// 解密 encryptedData
decryptedOpenID, err := DecryptAES128CBC(openID, sessionKey, iv)
if err != nil {
http.Error(w, fmt.Sprintf("解密失败: %v", err), http.StatusInternalServerError)
return
}
// 动态获取 access_token
accessToken, err := GetAccessToken()
if err != nil {
http.Error(w, fmt.Sprintf("获取 access_token 失败: %v", err), http.StatusInternalServerError)
return
}
fmt.Println("动态token为", accessToken)
// 构造请求体
requestBody := map[string]string{
"open_id": openID,
"mp_id": mpID,
"date_hour": dateHour,
"access_token": accessToken,
}
requestBodyJSON, err := json.Marshal(requestBody) // 转为 JSON 格式
if err != nil {
http.Error(w, "请求体编码失败", http.StatusInternalServerError)
return
}
// 向抖音 API 发起请求
apiURL := "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm" // 抖音 API 地址
resp, err := http.Post(apiURL, "application/json", bytes.NewBuffer(requestBodyJSON))
if err != nil {
http.Error(w, "调用抖音 API 失败", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// 读取抖音 API 的响应
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, "读取抖音 API 响应失败", http.StatusInternalServerError)
return
}
// 将抖音 API 的响应返回给客户端
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
w.Write(responseBody)
})
// 启动 HTTP 服务器
fmt.Println("服务器已启动http://localhost:8080")
err := http.ListenAndServe(":8080", nil) // 监听 8080 端口
if err != nil {
fmt.Println("服务器启动失败:", err)
}
}

BIN
sever/test/test.exe Normal file

Binary file not shown.