You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

66 lines
1.7 KiB

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