...

Source file src/google.golang.org/api/examples/youtube.go

Documentation: google.golang.org/api/examples

     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  
     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  // 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  //
    27  //	go build -o go-api-demo
    28  //	go-api-demo -clientid="my-clientid" -secret="my-secret" youtube filename
    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", // can not use non-alpha-numeric characters
    45  			CategoryId:  "22",
    46  		},
    47  		Status: &youtube.VideoStatus{PrivacyStatus: "unlisted"},
    48  	}
    49  
    50  	// The API returns a 400 Bad Request response if tags is an empty string.
    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