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("").SetClientSecret("") 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 }