Exemplo n.º 1
0
        public static async Task <string> GetVideo(string id, string part = ChannelPartParams.Default, string fields = ChannelFieldParams.Default)
        {
            StringBuilder url = new StringBuilder(YouTubeApi.ApiBase);

            url.Append("/videos?");
            url.Append("id=");
            url.Append(id);
            url.Append("&key=");
            url.Append(CredentialsManager.GetApiKey());
            if (!string.IsNullOrEmpty(fields))
            {
                url.Append("&fields=");
                url.Append(fields);
            }
            if (!string.IsNullOrEmpty(part))
            {
                url.Append("&part=");
                url.Append(part);
            }
            HttpResponseMessage httpResponseMessage = await HttpClient.GetAsync(url.ToString());

            string result = await httpResponseMessage.Content.ReadAsStringAsync();

            return(result);
        }
Exemplo n.º 2
0
        private static async Task <string> GetChannelItemsAsync(string type, ChannelSearchParams searchParams)
        {
            if (string.IsNullOrEmpty(type))
            {
                type = SearchTypeParams.Video;
            }
            StringBuilder url = new StringBuilder(YouTubeApi.ApiBase);

            url.Append("/search?");
            url.Append("key=");
            url.Append(CredentialsManager.GetApiKey());
            url.Append("&type=");
            url.Append(type);
            url.Append("&order=");
            url.Append("date");
            url.Append("&maxResults=");
            url.Append(50);
            if (!string.IsNullOrEmpty(searchParams.ChannelId))
            {
                url.Append("&channelId=");
                url.Append(searchParams.ChannelId);
            }
            if (!string.IsNullOrEmpty(searchParams.Keyword))
            {
                url.Append("&q=");
                url.Append(Uri.EscapeUriString(searchParams.Keyword));
            }
            if (!string.IsNullOrEmpty(searchParams.Fields))
            {
                url.Append("&fields=");
                url.Append(searchParams.Fields);
            }
            if (!string.IsNullOrEmpty(searchParams.Part))
            {
                url.Append("&part=");
                url.Append(searchParams.Part);
            }
            if (!string.IsNullOrEmpty(searchParams.NextPageToken))
            {
                url.Append("&pageToken=");
                url.Append(searchParams.NextPageToken);
            }
            HttpResponseMessage httpResponseMessage = await HttpClient.GetAsync(url.ToString());

            string result = await httpResponseMessage.Content.ReadAsStringAsync();

            return(result);
        }
Exemplo n.º 3
0
        private async Task Run()
        {
            BaseClientService.Initializer baseClientServiceInitializer = new BaseClientService.Initializer
            {
                ApiKey          = CredentialsManager.GetApiKey(),
                ApplicationName = GetType().ToString()
            };
            YouTubeService youtubeService = new YouTubeService(baseClientServiceInitializer);

            SearchResource.ListRequest searchListRequest = youtubeService.Search.List("snippet");
            searchListRequest.Q          = "Google"; // Replace with your search term.
            searchListRequest.MaxResults = 50;

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

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

            // Add each result to the appropriate list, and then display the lists of
            // matching videos, channels, and playlists.
            foreach (SearchResult searchResult in searchListResponse.Items)
            {
                switch (searchResult.Id.Kind)
                {
                case "youtube#video":
                    videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId));
                    break;

                case "youtube#channel":
                    channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
                    break;

                case "youtube#playlist":
                    playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
                    break;
                }
            }
            Console.WriteLine("Videos:\n{0}\n", string.Join("\n", videos));
            Console.WriteLine("Channels:\n{0}\n", string.Join("\n", channels));
            Console.WriteLine("Playlists:\n{0}\n", string.Join("\n", playlists));
        }
Exemplo n.º 4
0
        /// BaseClientService: A base class for a client service which provides common
        /// mechanism for all services, like  serialization and GZip support.
        /// It should be safe to use a single service instance to make server requests
        /// concurrently from multiple threads.
        /// Initializer: An initializer class for the client service
        private async Task Run()
        {
            // Create the service.
            BaseClientService.Initializer initializer = new BaseClientService.Initializer
            {
                ApplicationName = "Discovery Sample",
                ApiKey          = CredentialsManager.GetApiKey(),
            };
            DiscoveryService service = new DiscoveryService(initializer);

            // Run the request.
            Console.WriteLine("Executing a list request...");
            DirectoryList result = await service.Apis.List().ExecuteAsync();

            // Display the results.
            if (result.Items != null)
            {
                foreach (DirectoryList.ItemsData api in result.Items)
                {
                    Console.WriteLine(api.Id + " - " + api.Title);
                }
            }
        }