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


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

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

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

您应该在选定的播放列表中找到视频项的内部 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();
}

相关内容

  • 没有找到相关文章

最新更新