如何通过播放列表ID从Youtube播放列表中删除视频?

时间:2018-01-15 19:45:09

标签: c# api video youtube-api

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

newPlaylist.Id = "PLQQ-7Yr6sumTIG34_J1QmDIQkAoQZGBF4";


var newPlaylistItem = new PlaylistItem();
newPlaylistItem.Snippet = new PlaylistItemSnippet();
newPlaylistItem.Snippet.PlaylistId = newPlaylist.Id;
newPlaylistItem.Snippet.ResourceId = new ResourceId();
newPlaylistItem.Snippet.ResourceId.Kind = "youtube#video";
newPlaylistItem.Snippet.ResourceId.VideoId = "33v7dvN84Ms";
newPlaylistItem = await youtubeService.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync();

我可以使用上面的代码将视频添加到此播放列表中。但我无法从此播放列表中删除该视频。

如何从播放列表中删除视频?

1 个答案:

答案 0 :(得分:0)

您应该在选定的播放列表中找到视频项的内部ID,如下所示:

private static void RemoveVideo(YouTubeService youTubeService, string playlistId, string videoId)
{
    // Find the matching video item
    var videos = youTubeService.PlaylistItems.List("snippet");
    videos.PlaylistId = playlistId;

    var video = videos.Execute().Items.Where(a => a.Snippet.ResourceId.VideoId == videoId).SingleOrDefault();

    if (video == null)
    {
        throw new ArgumentException("Video not found!", nameof(videoId));
    }

    // Now delete it by it's inner ID
    var playlistInsertRequest = youTubeService.PlaylistItems.Delete(video.Id);
    playlistInsertRequest.Execute();
}