【问题标题】:How to get video status using YouTube Data API with C#如何使用带有 C# 的 YouTube 数据 API 获取视频状态
【发布时间】:2021-11-16 06:17:29
【问题描述】:

我在 3 天内尝试了代码以在我的 YouTube 频道中获取视频状态信息,但没有成功。

下面是我使用的代码,我可以writeln()视频的标题、id、描述,但无法获取视频状态。

private async Task Editvideo()
{
    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.
                // playlistItem.Status = new PlaylistItemStatus();
                // string videostatus = playlistItem.Status.PrivacyStatus;
                // Console.WriteLine(videostatus);
                Console.WriteLine("{0} ({1})", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
            }

            nextPageToken = playlistItemsListResponse.NextPageToken;
        }
    }
}

【问题讨论】:

    标签: c# api youtube-api youtube-data-api


    【解决方案1】:

    如果考虑到PlaylistItems.list API 端点的part 请求参数的规范,您的问题的答案是立竿见影的:

    part(字符串)

    part 参数指定 API 响应将包含的一个或多个 playlistItem 资源属性的逗号分隔列表。

    如果参数标识包含子属性的属性,则子属性将包含在响应中。例如,在playlistItem 资源中,snippet 属性包含许多字段,包括titledescriptionpositionresourceId 属性。因此,如果您设置part=snippet,API 响应将包含所有这些属性。

    以下列表包含您可以包含在参数值中的part 名称:

    • contentDetails
    • id
    • snippet
    • status

    现在,很明显,当需要端点返回status 属性时,part 也应该包含status,如下所示:

    var playlistItemsListRequest = youtubeService
            .PlaylistItems.List("snippet,status");            
    

    通过对代码的这种修改,从API获得的响应将包含playlistItem.Status下的查询对象。

    【讨论】:

    • 哦,确实,当我们需要返回的status 属性的结果时,part 需要包含它。正如您在上面所说的,我在part 中进行了更改,并且代码运行良好。非常感谢你
    猜你喜欢
    • 1970-01-01
    • 2018-06-28
    • 2017-05-16
    • 2014-07-29
    • 2015-09-06
    • 1970-01-01
    • 2012-12-20
    • 2012-07-04
    • 2011-11-25
    相关资源
    最近更新 更多