【问题标题】:c# get youtube playlist using Youtube data api v3c# 使用 Youtube data api v3 获取 youtube 播放列表
【发布时间】:2017-11-01 21:43:00
【问题描述】:

我目前正在为我的学校开发一个软件。该软件必须能够发布视频,但它还必须能够将这些视频添加到播放列表中。 问题:如何列出帐户的所有现有播放列表(甚至是空的或私有的)?我正在使用 YouTube 数据 API v3,但无法获取空的。 代码如下:

private async Task Run()
    {
        UserCredential credential;
        using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                // This OAuth 2.0 access scope allows for read-only access to the authenticated 
                // user's account, but not other types of account access.
                new[] { YouTubeService.Scope.YoutubeReadonly },
                "user",
                CancellationToken.None,
                new FileDataStore(this.GetType().ToString())
            );
        }

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = this.GetType().ToString()
        });

        var channelsListRequest = youtubeService.Channels.List("contentDetails");
        channelsListRequest.Mine = true;

        // Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
        var channelsListResponse = await channelsListRequest.ExecuteAsync();

        foreach (var channel in channelsListResponse.Items)
        {
            // From the API response, extract the playlist ID that identifies the list
            // of videos uploaded to the authenticated user's channel.
            var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;

            Console.WriteLine("Videos in list {0}", uploadsListId);

            var nextPageToken = "";
            while (nextPageToken != null)
            {
                var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
                playlistItemsListRequest.PlaylistId = uploadsListId;
                playlistItemsListRequest.MaxResults = 50;
                playlistItemsListRequest.PageToken = nextPageToken;

                // Retrieve the list of videos uploaded to the authenticated user's channel.
                var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();

                foreach (var playlistItem in playlistItemsListResponse.Items)
                {
                    // Print information about each video.
                    Console.WriteLine("{0} ({1})", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
                }

                nextPageToken = playlistItemsListResponse.NextPageToken;
            }
        }
    }

提前谢谢你。

【问题讨论】:

    标签: c# youtube youtube-data-api


    【解决方案1】:

    我通过这样做让这段代码工作:

    private async Task Run()
        {
            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows for read-only access to the authenticated 
                    // user's account, but not other types of account access.
                    new[] { YouTubeService.Scope.YoutubeReadonly },
                    "user",
                    CancellationToken.None,
                    new FileDataStore(this.GetType().ToString())
                );
            }
    
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = this.GetType().ToString()
            });
    
            // var channelsListRequest = youtubeService.Channels.List("contentDetails");
            var playlistListRequest = youtubeService.Playlists.List("snippet");
            playlistListRequest.Mine = true;
    
            // Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
            var playlistListResponse = await playlistListRequest.ExecuteAsync();
    
            foreach (var playlist in playlistListResponse.Items)
            {
                // From the API response, extract the playlist ID that identifies the list
                // of videos uploaded to the authenticated user's channel.
                var playlistListId = playlist.Id;
    
                Console.WriteLine("Videos in list {0}", playlistListId);
            }
        }
    

    感谢“not_a_bot”帮助我。

    【讨论】:

    • 我正在尝试做同样的事情,但它失败了。 "snippet" 魔术字符串是什么?
    • 您需要更改请求类型。
    • 示例:'var channelsListRequest = youtubeService.Channels.List("contentDetails"); channelsListRequest.Mine = true;'变成:'var playlistListRequest = youtubeService.Playlists.List("sn-p"); playlistListRequest.Mine = true;' @VioletGiraffe
    • 希望这对您的项目有所帮助。
    • 谢谢!我很困惑,直到我意识到我可能需要 RTFM。这回答了我的问题:)
    猜你喜欢
    • 2013-09-19
    • 2017-01-05
    • 2015-02-26
    • 2015-04-23
    • 2014-06-04
    • 2014-02-12
    • 2016-09-17
    • 2015-08-18
    • 1970-01-01
    相关资源
    最近更新 更多