2017-05-31 211 views
1

我目前正在爲我的學校開發一款軟件。該軟件必須能夠發佈視頻,但它也必須能夠將這些視頻添加到播放列表中。 問題:如何列出帳戶的所有現有播放列表(甚至是空的或私人的)?我正在使用YouTube數據API v3,並且我無法獲得空的。 這裏是代碼:c#使用Youtube數據API獲取youtube播放列表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; 
      } 
     } 
    } 

在此先感謝您。

回答

0

我得到這個代碼做這個工作:

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

感謝「not_a_bot」對我的幫助。

+0

我試圖做同樣的事情,但它失敗了。什麼是''snippet''魔術串? –

+0

您需要更改請求類型。 – SuperCoder

+0

示例:'var channelsListRequest = youtubeService.Channels.List(「contentDetails」); channelsListRequest.Mine = true;'成爲:'var playlistListRequest = youtubeService.Playlists.List(「snippet」); playlistListRequest.Mine = true;' @VioletGiraffe – SuperCoder

相關問題