...
1
2
3
4
5 package main
6
7 import (
8 "fmt"
9 "log"
10 "net/http"
11 "os"
12
13 youtube "google.golang.org/api/youtube/v3"
14 )
15
16 func init() {
17 registerDemo("youtube", youtube.YoutubeUploadScope, youtubeMain)
18 }
19
20
21
22
23
24
25
26
27
28
29 func youtubeMain(client *http.Client, argv []string) {
30 if len(argv) < 1 {
31 fmt.Fprintln(os.Stderr, "Usage: youtube filename")
32 return
33 }
34 filename := argv[0]
35
36 service, err := youtube.New(client)
37 if err != nil {
38 log.Fatalf("Unable to create YouTube service: %v", err)
39 }
40
41 upload := &youtube.Video{
42 Snippet: &youtube.VideoSnippet{
43 Title: "Test Title",
44 Description: "Test Description",
45 CategoryId: "22",
46 },
47 Status: &youtube.VideoStatus{PrivacyStatus: "unlisted"},
48 }
49
50
51 upload.Snippet.Tags = []string{"test", "upload", "api"}
52
53 call := service.Videos.Insert([]string{"snippet", "status"}, upload)
54
55 file, err := os.Open(filename)
56 if err != nil {
57 log.Fatalf("Error opening %v: %v", filename, err)
58 }
59 defer file.Close()
60
61 response, err := call.Media(file).Do()
62 if err != nil {
63 log.Fatalf("Error making YouTube API call: %v", err)
64 }
65 fmt.Printf("Upload successful! Video ID: %v\n", response.Id)
66 }
67
View as plain text