Home > other >  How to get video status using YouTube Data API with C#
How to get video status using YouTube Data API with C#

Time:09-24

I have tried code in 3 days for get video status info in my YouTube channel, but did not succeed.

Below is the code I used, I can writeln() the title, id, description of the video, but cannot get the video status.

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

CodePudding user response:

The answer to your question is immediate, if taking into account the specification of the part request parameter of the PlaylistItems.list API endpoint:

part (string)

The part parameter specifies a comma-separated list of one or more playlistItem resource properties that the API response will include.

If the parameter identifies a property that contains child properties, the child properties will be included in the response. For example, in a playlistItem resource, the snippet property contains numerous fields, including the title, description, position, and resourceId properties. As such, if you set part=snippet, the API response will contain all of those properties.

The following list contains the part names that you can include in the parameter value:

  • contentDetails
  • id
  • snippet
  • status

Now, it should be obvious that when needing the status property being returned by the endpoint, part should contain status too as show below:

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

By this change to the code, the response obtained from the API will include the queried object under playlistItem.Status.

  • Related