修改解析
This commit is contained in:
parent
9c94786d7c
commit
91aedfe56b
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
@ -9,11 +8,34 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
credential "github.com/bytedance/douyin-openapi-credential-go/client"
|
credential "github.com/bytedance/douyin-openapi-credential-go/client"
|
||||||
openApiSdkClient "github.com/bytedance/douyin-openapi-sdk-go/client"
|
openApiSdkClient "github.com/bytedance/douyin-openapi-sdk-go/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type APIResponse struct {
|
||||||
|
BaseResp struct {
|
||||||
|
StatusCode int `json:"StatusCode"`
|
||||||
|
StatusMessage string `json:"StatusMessage"`
|
||||||
|
} `json:"BaseResp"`
|
||||||
|
Data struct {
|
||||||
|
Records []struct {
|
||||||
|
Aid string `json:"aid"`
|
||||||
|
Cost int `json:"cost"`
|
||||||
|
Did string `json:"did"`
|
||||||
|
EventName string `json:"event_name"`
|
||||||
|
EventTime string `json:"event_time"`
|
||||||
|
OpenID string `json:"open_id"`
|
||||||
|
ID int `json:"id"`
|
||||||
|
} `json:"records"`
|
||||||
|
Total int `json:"total"`
|
||||||
|
} `json:"data"`
|
||||||
|
ErrMsg string `json:"err_msg"`
|
||||||
|
ErrNo int `json:"err_no"`
|
||||||
|
LogID string `json:"log_id"`
|
||||||
|
}
|
||||||
|
|
||||||
// 获取 access_token 的函数
|
// 获取 access_token 的函数
|
||||||
func GetAccessToken() (string, error) {
|
func GetAccessToken() (string, error) {
|
||||||
// 初始化 SDK 客户端,设置 app_id 和 secret
|
// 初始化 SDK 客户端,设置 app_id 和 secret
|
||||||
@ -122,43 +144,47 @@ func main() {
|
|||||||
// 从请求中获取参数
|
// 从请求中获取参数
|
||||||
openID := r.URL.Query().Get("open_id") // 获取 open_id
|
openID := r.URL.Query().Get("open_id") // 获取 open_id
|
||||||
mpID := r.URL.Query().Get("mp_id") // 获取 mp_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
|
dateHour := r.URL.Query().Get("date_hour") // 获取 date_hour
|
||||||
|
pageNo := r.URL.Query().Get("pageNo") // 获取 date_hour
|
||||||
|
pageSize := r.URL.Query().Get("pageSize") // 获取 date_hour
|
||||||
|
|
||||||
// 检查是否有缺少参数
|
// 检查是否有缺少参数
|
||||||
if openID == "" || mpID == "" || dateHour == "" {
|
if mpID == "" || dateHour == "" {
|
||||||
http.Error(w, "缺少必要的参数", http.StatusBadRequest)
|
http.Error(w, "缺少必要的参数", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 解密 encryptedData
|
|
||||||
decryptedOpenID, err := DecryptAES128CBC(openID, sessionKey, iv)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, fmt.Sprintf("解密失败: %v", err), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// 动态获取 access_token
|
// 动态获取 access_token
|
||||||
accessToken, err := GetAccessToken()
|
accessToken, err := GetAccessToken()
|
||||||
|
apiURL := "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm"
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("获取 access_token 失败: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("获取 access_token 失败: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("动态token为", accessToken)
|
fmt.Println("动态token为", accessToken)
|
||||||
|
params := url.Values{}
|
||||||
|
params.Add("open_id", openID)
|
||||||
|
params.Add("mp_id", mpID)
|
||||||
|
params.Add("date_hour", dateHour)
|
||||||
|
params.Add("access_token", accessToken)
|
||||||
|
params.Add("page_no", pageNo)
|
||||||
|
params.Add("page_size", pageSize)
|
||||||
// 构造请求体
|
// 构造请求体
|
||||||
requestBody := map[string]string{
|
// requestBody := map[string]string{
|
||||||
"open_id": openID,
|
// "open_id": openID,
|
||||||
"mp_id": mpID,
|
// "mp_id": mpID,
|
||||||
"date_hour": dateHour,
|
// "date_hour": dateHour,
|
||||||
"access_token": accessToken,
|
// "access_token": accessToken,
|
||||||
}
|
// "page_no": pageNo,
|
||||||
requestBodyJSON, err := json.Marshal(requestBody) // 转为 JSON 格式
|
// "page_size": pageSize,
|
||||||
if err != nil {
|
// }
|
||||||
http.Error(w, "请求体编码失败", http.StatusInternalServerError)
|
fullURL := fmt.Sprintf("%s?%s", apiURL, params.Encode())
|
||||||
return
|
// requestBodyJSON, err := json.Marshal(requestBody) // 转为 JSON 格式
|
||||||
}
|
// if err != nil {
|
||||||
|
// http.Error(w, "请求体编码失败", http.StatusInternalServerError)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
// 向抖音 API 发起请求
|
resp, err := http.Get(fullURL)
|
||||||
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 {
|
if err != nil {
|
||||||
http.Error(w, "调用抖音 API 失败", http.StatusInternalServerError)
|
http.Error(w, "调用抖音 API 失败", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -171,11 +197,92 @@ func main() {
|
|||||||
http.Error(w, "读取抖音 API 响应失败", http.StatusInternalServerError)
|
http.Error(w, "读取抖音 API 响应失败", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var apiResponse APIResponse
|
||||||
|
err = json.Unmarshal(responseBody, &apiResponse)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error while parsing response JSON: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if apiResponse.ErrNo != 0 {
|
||||||
|
fmt.Printf("API Error: %s (ErrNo: %d)\n", apiResponse.ErrMsg, apiResponse.ErrNo)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
totalCost := 0
|
||||||
|
totalRecords := len(apiResponse.Data.Records)
|
||||||
|
|
||||||
|
for _, record := range apiResponse.Data.Records {
|
||||||
|
totalCost += record.Cost
|
||||||
|
}
|
||||||
|
|
||||||
|
if totalRecords == 0 {
|
||||||
|
fmt.Println("No records found. Unable to calculate ECPM.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ecpm := float64(totalCost) / 100000 * 1000 / float64(totalRecords)
|
||||||
|
fmt.Println("返回值为", responseBody)
|
||||||
|
// 根据key返回相应的值(这里可以替换为实际的业务逻辑)
|
||||||
|
value := fmt.Sprintf("Value for key '%s'", ecpm)
|
||||||
// 将抖音 API 的响应返回给客户端
|
// 将抖音 API 的响应返回给客户端
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(resp.StatusCode)
|
w.WriteHeader(resp.StatusCode)
|
||||||
w.Write(responseBody)
|
w.Write([]byte(value))
|
||||||
|
})
|
||||||
|
http.HandleFunc("/Login", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// 检查请求方法是否为GET
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从请求参数中获取"code"和"Anonymous"
|
||||||
|
code := r.URL.Query().Get("code")
|
||||||
|
Anonymous := r.URL.Query().Get("Anonymous")
|
||||||
|
|
||||||
|
fmt.Println("code:", code)
|
||||||
|
fmt.Println("Anonymous:", Anonymous)
|
||||||
|
|
||||||
|
// 参数校验
|
||||||
|
if code == "" {
|
||||||
|
http.Error(w, "Missing 'code' or 'Anonymous' parameter", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化 SDK 客户端
|
||||||
|
opt := new(credential.Config).
|
||||||
|
SetClientKey("tt8b32fd8f14071db707").
|
||||||
|
SetClientSecret("44018e80b1bde34395a52de67ce1e0c37c572d80")
|
||||||
|
|
||||||
|
sdkClient, err := openApiSdkClient.NewClient(opt)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("sdk init err:", err)
|
||||||
|
http.Error(w, "SDK 初始化失败", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建请求参数
|
||||||
|
sdkRequest := &openApiSdkClient.V2Jscode2sessionRequest{}
|
||||||
|
sdkRequest.SetAnonymousCode(Anonymous)
|
||||||
|
sdkRequest.SetAppid("tt8b32fd8f14071db707")
|
||||||
|
sdkRequest.SetCode(code)
|
||||||
|
sdkRequest.SetSecret("44018e80b1bde34395a52de67ce1e0c37c572d80")
|
||||||
|
|
||||||
|
// 调用 SDK
|
||||||
|
sdkResponse, err := sdkClient.V2Jscode2session(sdkRequest)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("sdk call err:", err)
|
||||||
|
http.Error(w, "SDK 调用失败", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印登录数据
|
||||||
|
fmt.Println("登录数据:", sdkResponse)
|
||||||
|
|
||||||
|
// 返回响应
|
||||||
|
value := fmt.Sprintf("Session Data: %v", sdkResponse.Data)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(value))
|
||||||
})
|
})
|
||||||
|
|
||||||
// 启动 HTTP 服务器
|
// 启动 HTTP 服务器
|
||||||
|
Loading…
x
Reference in New Issue
Block a user