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) } }