I am trying to upload a youtube video using Go. I do get a response of 200 OK but the video shows as processing abandoned on youtube studio
This is my full code:
func UploadVideo(w http.ResponseWriter, r *http.Request) { youtubeAuthToken := r.Header.Get("Authorization") const maxFileSize = 50 * 1024 * 1024 * 1024 // 50 GB r.ParseMultipartForm(maxFileSize) //file, handler, err := r.FormFile("videoFile") // //if err != nil { // // Handle the error // http.Error(w, "Failed to get video file", http.StatusBadRequest) // return //} //defer file.Close() // FOR TESTING filePath := "D:\\feed-prototype\\internal\\services\\video.mp4" // Specify the path to your video file file, err := os.Open(filePath) if err != nil { http.Error(w, "Failed to open the video file", http.StatusInternalServerError) return } defer file.Close() // FOR TESTING // creating a buffer to store the form data body := &bytes.Buffer{} writer := multipart.NewWriter(body) // Adding video file to the request part, err := writer.CreateFormFile("video/*", "video.mp4") // use handler.FileName when formData if err != nil { http.Error(w, "Failed to create form file", http.StatusInternalServerError) return } _, err = io.Copy(part, file) if err != nil { http.Error(w, "Failed to copy file data", http.StatusInternalServerError) return } // Video meta data var videoData utils.VideoData videoData.Snippet.Title = "My Video Title" videoData.Snippet.Description = "My Video Description" videoData.Snippet.Tags = []string{"tag1", "tag2", "tag3"} videoData.Snippet.CategoryId = "22" // Specify the category ID for your video videoData.Snippet.DefaultLanguage = "en-US" videoData.Status.PrivacyStatus = "private" metadata, err := json.Marshal(videoData) if err != nil { http.Error(w, "Failed to marshal video metadata", http.StatusInternalServerError) return } metaPart, err := writer.CreateFormField("json") if err != nil { http.Error(w, "Failed to create form field", http.StatusInternalServerError) return } _, err = metaPart.Write(metadata) if err != nil { http.Error(w, "Failed to write form field data", http.StatusInternalServerError) return } err = writer.Close() if err != nil { http.Error(w, "Failed to close multipart writer", http.StatusInternalServerError) return } // Creating HTTP request req, err := http.NewRequest("POST", "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet&part=status"+"&key="+apiKey+"&access_token="+youtubeAuthToken, body) if err != nil { http.Error(w, "Failed to create request", http.StatusInternalServerError) return } req.Header.Set("Content-Type", "application/octet-stream") req.Header.Set("Accept", "application/json") // Sending the request client := &http.Client{} resp, err := client.Do(req) if err != nil { http.Error(w, "Failed to send request", http.StatusInternalServerError) return } defer resp.Body.Close() responseBody, err := io.ReadAll(resp.Body) if err != nil { http.Error(w, "Failed to read response", http.StatusInternalServerError) return } // Handle the response if resp.StatusCode != http.StatusOK { fmt.Println(resp) http.Error(w, "Failed to upload video: "+resp.Status, http.StatusInternalServerError) return } // Process the response body as needed fmt.Println(string(responseBody)) // Return a success message w.WriteHeader(http.StatusOK) w.Write([]byte("Video uploaded successfully"))}
I manually take the video from the same directory
I am getting this response:
"kind": "youtube#video","etag": "sZkdG8TxLSa-eDstdXTs9Iw887I","id": "16wibcpe9ts","snippet": {"publishedAt": "2023-06-06T14:00:05Z","channelId": "UCKlDRAs3yyCLTqhVnrCrVBQ","title": "unknown","description": "","license": "youtube","embeddable": true,"publicStatsViewable": true }}
Could anyone help me out what the problem is. Am I properly sending the video file and the metadata to the YouTube API? Video is in mp4 format and is playable on my computer. It's just 3 seconds long.