Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#使用youtube数据api v3获取youtube播放列表_C#_Youtube_Youtube Data Api - Fatal编程技术网

c#使用youtube数据api v3获取youtube播放列表

c#使用youtube数据api v3获取youtube播放列表,c#,youtube,youtube-data-api,C#,Youtube,Youtube Data Api,我目前正在为我的学校开发一个软件。该软件必须能够发布视频,但也必须能够将这些视频添加到播放列表中。 问题:如何列出帐户的所有现有播放列表(即使是空的或私有的)?我使用的是YouTube数据API v3,我无法获取空的数据。 代码如下: private async Task Run() { UserCredential credential; using (var stream = new FileStream("client_secrets.json",

我目前正在为我的学校开发一个软件。该软件必须能够发布视频,但也必须能够将这些视频添加到播放列表中。 问题:如何列出帐户的所有现有播放列表(即使是空的或私有的)?我使用的是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;
            }
        }
    }

提前感谢。

我通过以下操作获得了此代码:

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);
        }
    }

多亏了“非机器人”的帮助。

我也在尝试,但失败了。什么是
“snippet”
魔术字符串?您需要更改请求类型。例如:'var channelsListRequest=youtubeService.Channels.List(“contentDetails”);channelsListRequest.my=true;'变成:'var playlistrequest=youtubeService.Playlists.List(“片段”);playlistrequest.Mine=true;'@VioletGiraffe希望这将有助于您的项目。谢谢!我很困惑,直到我意识到我可能需要RTFM。这回答了我的问题:)