Exemple #1
0
        public async Task <IList <ResponseSearchItem> > Search(string text)
        {
            var youtube = new Google.Apis.YouTube.v3.YouTubeService(new Google.Apis.Services.BaseClientService.Initializer()
            {
                HttpClientInitializer = Autenticar()
            });

            SearchResource.ListRequest listRequest = youtube.Search.List("snippet");

            listRequest.Q          = text;
            listRequest.MaxResults = 10;
            listRequest.Order      = SearchResource.ListRequest.OrderEnum.Relevance;

            SearchListResponse searchResponse = await listRequest.ExecuteAsync();

            return(searchResponse.Items.Select(item =>
                                               new ResponseSearchItem
            {
                Name = item.Snippet.Title,
                Url = item.Snippet.Thumbnails.Default__.Url,
                Type = item.Id.Kind,
                VideoId = item.Id.VideoId
            }
                                               ).ToList());
        }
 public YouTubeClient(IConfiguration configuration)
 {
     _service = new Google.Apis.YouTube.v3.YouTubeService(new BaseClientService.Initializer()
     {
         ApiKey          = configuration.GetSection("YouTubeConfig").GetSection("APIKEY").Value,
         ApplicationName = configuration.GetSection("YouTubeConfig").GetSection("ClientApplicationName").Value
     });
 }
 public YoutubeAPIClient(string apiKey)
 {
     _service = new YouTubeService(new BaseClientService.Initializer()
     {
         ApiKey          = apiKey,
         ApplicationName = this.GetType().ToString()
     });
 }
Exemple #4
0
        public async Task <string> Run()
        {
            // Create the service.
            var youtubeService = new Google.Apis.YouTube.v3.YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey          = "AIzaSyBaEizFXx0bkO7AhQaiYzwvG9-hv2STMNo",
                ApplicationName = this.GetType().ToString()
            });

            var searchListRequest = youtubeService.Search.List("snippet");

            searchListRequest.Q          = textBox1.Text; // Replace with your search term.
            searchListRequest.MaxResults = 50;
            int resultCount = 0;                          //結果大小
            int Max_result  = 40;                         //最大獲得數

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

            videos   = new List <string>();
            channels = new List <string>();

            videoTitle.Clear();
            videoId.Clear();



            // Add each result to the appropriate list, and then display the lists of
            // matching videos, channels, and playlists.
            foreach (var searchResult in searchListResponse.Items)
            {
                if (resultCount >= Max_result)
                {
                    break;
                }
                else
                {
                    switch (searchResult.Id.Kind)
                    {
                    case "youtube#video":
                        videoTitle.Add(searchResult.Snippet.Title);                 //儲存搜尋結果
                        videoId.Add(searchResult.Id.VideoId);
                        resultCount++;
                        break;
                    }
                }
            }
            //       textBox2.Text += String.Format("videoTitle:\r\n{0}\r\n", string.Join("\r\n", videoTitle));
            //       textBox2.Text += "\r\n";
            //      textBox2.Text += String.Format("videoId:\r\n{0}\r\n", string.Join("\r\n", videoId));

            return("111");
        }
        private static Google.Apis.YouTube.v3.YouTubeService AuthorizeService()
        {
            UserCredential credentials;

            using (var stream = new FileStream("YoutubeAPI.json", FileMode.Open, FileAccess.Read)) {
                credentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeReadonly },
                    "user",
                    CancellationToken.None,
                    new FileDataStore("VideoAPI")
                    ).Result;
            }

            var ytService = new Google.Apis.YouTube.v3.YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credentials,
                ApplicationName       = "The Offside Club"
            });

            return(ytService);
        }
        public async Task<string> Run()
        {
            // Create the service.
            var youtubeService = new Google.Apis.YouTube.v3.YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = "AIzaSyBaEizFXx0bkO7AhQaiYzwvG9-hv2STMNo",
                ApplicationName = this.GetType().ToString()
            });

            var searchListRequest = youtubeService.Search.List("snippet");
            searchListRequest.Q = textBox1.Text; // Replace with your search term.
            searchListRequest.MaxResults = 50;
            int resultCount=0;      //結果大小
            int Max_result=40;       //最大獲得數

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

            videos = new List<string>();
            channels = new List<string>();

            videoTitle.Clear();
            videoId.Clear();



            // Add each result to the appropriate list, and then display the lists of
            // matching videos, channels, and playlists.
            foreach (var searchResult in searchListResponse.Items)
            {

                if (resultCount >= Max_result)
                    break;
                else
                {
                    switch (searchResult.Id.Kind)
                    {
                        case "youtube#video":
                            videoTitle.Add(searchResult.Snippet.Title);             //儲存搜尋結果
                            videoId.Add(searchResult.Id.VideoId);
                            resultCount++;
                            break;

                    }
                }
            }
     //       textBox2.Text += String.Format("videoTitle:\r\n{0}\r\n", string.Join("\r\n", videoTitle));
     //       textBox2.Text += "\r\n";
      //      textBox2.Text += String.Format("videoId:\r\n{0}\r\n", string.Join("\r\n", videoId));

            return "111";
        }
Exemple #7
0
 public YoutubeAPIClient(YouTubeService service)
 {
     _service = service;
 }