2015-12-07 217 views

回答

10

我已經在這樣做的話,它的工作對我來說 我已經使用的Youtube API V3從的NuGet包管理

using Google.Apis.Services; 
using Google.Apis.YouTube.v3; 

public ActionResult GetVideo(YouTubeData objYouTubeData) 
{ 
    try 
    { 
     var yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "Your API Key" }); 
     var channelsListRequest = yt.Channels.List("contentDetails"); 
     channelsListRequest.ForUsername = "kkrofficial"; 
     var channelsListResponse = channelsListRequest.Execute(); 
     foreach (var channel in channelsListResponse.Items) 
     { 
      // of videos uploaded to the authenticated user's channel. 
      var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads; 
      var nextPageToken = ""; 
      while (nextPageToken != null) 
      { 
       var playlistItemsListRequest = yt.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 = playlistItemsListRequest.Execute(); 
       foreach (var playlistItem in playlistItemsListResponse.Items) 
       { 
        // Print information about each video. 
        //Console.WriteLine("Video Title= {0}, Video ID ={1}", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId); 
        var qry = (from s in ObjEdbContext.ObjTubeDatas where s.Title == playlistItem.Snippet.Title select s).FirstOrDefault(); 
        if (qry == null) 
        { 
         objYouTubeData.VideoId = "https://www.youtube.com/embed/" + playlistItem.Snippet.ResourceId.VideoId; 
         objYouTubeData.Title = playlistItem.Snippet.Title; 
         objYouTubeData.Descriptions = playlistItem.Snippet.Description; 
         objYouTubeData.ImageUrl = playlistItem.Snippet.Thumbnails.High.Url; 
         objYouTubeData.IsValid = true; 
         ObjEdbContext.ObjTubeDatas.Add(objYouTubeData); 
         ObjEdbContext.SaveChanges(); 
         ModelState.Clear(); 

        } 
       } 
       nextPageToken = playlistItemsListResponse.NextPageToken; 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     ViewBag.ErrorMessage = "Some exception occured" + e; 
     return RedirectToAction("GetYouTube"); 
    } 

    return RedirectToAction("GetYouTube"); 
} 

在這一行

channelsListRequest.ForUsername = "kkrofficial"; //kkrofficial is kkr channel name. 

按照此鏈接提供的頻道名稱 https://developers.google.com/youtube/v3/code_samples/dotnet#retrieve_my_uploads

+0

什麼ClientServiceRequest.cs?我應該有這個嗎? – Cieja

+0

我已經創建了API,但在我的應用程序中收到錯誤消息:Access Not Configured。 API(YouTube數據API)未針對您的項目啓用。請使用Google Developers Console更新您的配置。 [403] – Cieja

+0

ClientServiceRequest.cs不需要...只需從Nuget Packet Manager的Youtube API v3安裝該設置 –

3

你可以使用你的API密鑰,只是查詢所有頻道的視頻(即使不是你的:))

 public Task<List<SearchResult>> GetVideosFromChannelAsync(string ytChannelId) 
    { 

     return Task.Run(() => 
     { 
      List<SearchResult> res = new List<SearchResult>(); 

      string nextpagetoken = " "; 

      while (nextpagetoken != null) 
      { 
       var searchListRequest = _youtubeService.Search.List("snippet"); 
       searchListRequest.MaxResults = 50; 
       searchListRequest.ChannelId = ytChannelId; 
       searchListRequest.PageToken = nextpagetoken; 
       searchListRequest.Type  = "video"; 

       // Call the search.list method to retrieve results matching the specified query term. 
       var searchListResponse = searchListRequest.Execute(); 

       // Process the video responses 
       res.AddRange(searchListResponse.Items); 

       nextpagetoken = searchListResponse.NextPageToken; 

      } 

      return res; 

     }); 
    } 

這個方法應該讓你的軌道上

相關問題