Go用のGoogle APIのクライアントライブラリ を使用した次のコード サンプルは YouTube Data API で利用可能です。これらのコードサンプルは GithubのYouTube APIコードサンプルレポジトリ 内にあるgo
のフォルダからダウンロードできます。
マイ アップロード動画の取得
次のコード サンプルでは、API の playlistItems.list
メソッドを呼び出し、リクエストに関連付けられているチャンネルにアップロ>ードされた動画のリストを取得します。また、mine
パラメータを true
に設定して channels.list
メソッドを呼び出し、チャンネルのアップロード済み
動画を識別する再生リスト ID を取得します。
package main import ( "flag" "fmt" "log" "google.golang.org/api/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() } } }