1// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8	"fmt"
9	"log"
10	"net/http"
11	"os"
12
13	youtube "google.golang.org/api/youtube/v3"
14)
15
16func init() {
17	registerDemo("youtube", youtube.YoutubeUploadScope, youtubeMain)
18}
19
20// youtubeMain is an example that demonstrates calling the YouTube API.
21// It is similar to the sample found on the Google Developers website:
22// https://developers.google.com/youtube/v3/docs/videos/insert
23// but has been modified slightly to fit into the examples framework.
24//
25// Example usage:
26//   go build -o go-api-demo
27//   go-api-demo -clientid="my-clientid" -secret="my-secret" youtube filename
28func youtubeMain(client *http.Client, argv []string) {
29	if len(argv) < 1 {
30		fmt.Fprintln(os.Stderr, "Usage: youtube filename")
31		return
32	}
33	filename := argv[0]
34
35	service, err := youtube.New(client)
36	if err != nil {
37		log.Fatalf("Unable to create YouTube service: %v", err)
38	}
39
40	upload := &youtube.Video{
41		Snippet: &youtube.VideoSnippet{
42			Title:       "Test Title",
43			Description: "Test Description", // can not use non-alpha-numeric characters
44			CategoryId:  "22",
45		},
46		Status: &youtube.VideoStatus{PrivacyStatus: "unlisted"},
47	}
48
49	// The API returns a 400 Bad Request response if tags is an empty string.
50	upload.Snippet.Tags = []string{"test", "upload", "api"}
51
52	call := service.Videos.Insert("snippet,status", upload)
53
54	file, err := os.Open(filename)
55	if err != nil {
56		log.Fatalf("Error opening %v: %v", filename, err)
57	}
58	defer file.Close()
59
60	response, err := call.Media(file).Do()
61	if err != nil {
62		log.Fatalf("Error making YouTube API call: %v", err)
63	}
64	fmt.Printf("Upload successful! Video ID: %v\n", response.Id)
65}
66