【问题标题】:How can i make that when searching for youtube uploads using google api v3 it will show more then 50 results?在使用 google api v3 搜索 youtube 上传时,我该如何做到这一点,它会显示超过 50 个结果?
【发布时间】:2015-12-02 23:07:06
【问题描述】:

如果我将最大结果设为 400,它将产生异常,因为它应该在 0-50 到 400 之间。 所以现在为了测试我做了 50 次。

我怎样才能让当它找到 50 个结果并将它们添加到列表中时,它会再次搜索下一个 50 个结果,依此类推直到结束?

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;


namespace Automatic_Record
{


    class Youtube_Retrieve_Uploads
    {
        public Youtube_Retrieve_Uploads()
        {
            test();
            /*try
            {
                Run().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
            }*/
        }

        private void test()
        {
            YouTubeService yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "AIzaSyDcqx8nMWQL9wshpAs0Q-h0twpGd6R1BJM" });
            List<string> videos = new List<string>();

            var searchListRequest = yt.Search.List("snippet");
            searchListRequest.ChannelId = "UCUE_2qgW-nJOjOlO28uZHtQ";//"UCbe-iLd-TEl3_YQaNSm8dng";
            searchListRequest.MaxResults = 50;
            var searchListResult = searchListRequest.Execute();

            foreach (var item in searchListResult.Items)
            {
                if (item.Snippet.Title.StartsWith("Gta"))
                {
                    videos.Add("ID: " + item.Id.VideoId);
                    videos.Add("SNIPPET: " + item.Snippet.Title);
                }
            }  
        }

这工作正常,但我只得到 50 个结果。我想从 Gta 开始获取整个视频,但一般来说,我想获得前 50 个结果,然后是下一个 50 个,然后是下一个 50 个,直到没有更多结果。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    您需要使用令牌来循环浏览页面,以识别您所在的页面。

    通过这个例子跟随 nextPageToken 变量:

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

    有关更多帮助和示例,请参阅此链接: https://developers.google.com/youtube/v3/code_samples/dotnet

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2015-10-23
    相关资源
    最近更新 更多