forked from youtube/api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_uploads.go
73 lines (60 loc) · 1.95 KB
/
my_uploads.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"flag"
"fmt"
"log"
"code.google.com/p/google-api-go-client/youtube/v3"
)
func main() {
flag.Parse()
client, err := buildOAuthHTTPClient(youtube.YoutubeReadonlyScope)
if err != nil {
log.Fatalf("Error building OAuth client: %v", err)
}
service, err := youtube.New(client)
if err != nil {
log.Fatalf("Error creating YouTube client: %v", err)
}
// Start making YouTube API calls.
// Call the channels.list method. Set the mine parameter to true to
// retrieve the playlist ID for uploads to the authenticated user's
// channel.
call := service.Channels.List("contentDetails").Mine(true)
response, err := call.Do()
if err != nil {
// The channels.list method call returned an error.
log.Fatalf("Error making API call to list channels: %v", err.Error())
}
for _, channel := range response.Items {
playlistId := channel.ContentDetails.RelatedPlaylists.Uploads
// Print the playlist ID for the list of uploaded videos.
fmt.Printf("Videos in list %s\r\n", playlistId)
nextPageToken := ""
for {
// Call the playlistItems.list method to retrieve the
// list of uploaded videos. Each request retrieves 50
// videos until all videos have been retrieved.
playlistCall := service.PlaylistItems.List("snippet").
PlaylistId(playlistId).
MaxResults(50).
PageToken(nextPageToken)
playlistResponse, err := playlistCall.Do()
if err != nil {
// The playlistItems.list method call returned an error.
log.Fatalf("Error fetching playlist items: %v", err.Error())
}
for _, playlistItem := range playlistResponse.Items {
title := playlistItem.Snippet.Title
videoId := playlistItem.Snippet.ResourceId.VideoId
fmt.Printf("%v, (%v)\r\n", title, videoId)
}
// Set the token to retrieve the next page of results
// or exit the loop if all results have been retrieved.
nextPageToken = playlistResponse.NextPageToken
if nextPageToken == "" {
break
}
fmt.Println()
}
}
}