The following topics were found:
''' # Display the HTML page listing the topic choices. self.response.out.write(select_topic_page) def post(self): topic_id = self.request.get('topic') # Service for calling the YouTube API youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=API_KEY) # Execute the search request using default query term and retrieved topic. search_response = youtube.search().list( part = 'id,snippet', type = 'video', topicId = topic_id ).execute() videos = [] for search_result in search_response.get("items", []): videos.append(search_result) template_values = { 'videos': videos } self.response.headers['Content-type'] = 'text/html' template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render(template_values)) app = webapp2.WSGIApplication([ ('/.*', MainHandler), ], debug=True) ``` ### Retrieve a channel's uploads The code sample below calls the API's `playlistItems.list` method to retrieve a list of videos uploaded to a specified channel. The channel can be identified by its channel ID or channel name. The code also calls the `channels.list` method to retrieve the playlist ID that identifies the channel's uploaded videos. ```python import os import urllib import webapp2 import jinja2 from apiclient.discovery import build from optparse import OptionParser import json JINJA_ENVIRONMENT = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), extensions=['jinja2.ext.autoescape']) REGISTRATION_INSTRUCTIONS = """ You must set up a project and get an API key to run this code. Please see the instructions for creating a project and a key at https://developers.google.com/youtube/registering_an_application.Which channel's videos do you want to see?
''' # Display the HTML page that shows the form. self.response.out.write(select_channel_page) def post(self): # Service for calling the YouTube API youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=API_KEY) # Use form inputs to create request params for channel details channel_type = self.request.get('channel_type') channels_response = None if channel_type == 'id': channels_response = youtube.channels().list( id=self.request.get('channel'), part='snippet,contentDetails' ).execute() else: channels_response = youtube.channels().list( forUsername=self.request.get('channel'), part='snippet,contentDetails' ).execute() channel_name = '' videos = [] for channel in channels_response['items']: uploads_list_id = channel['contentDetails']['relatedPlaylists']['uploads'] channel_name = channel['snippet']['title'] next_page_token = '' while next_page_token is not None: playlistitems_response = youtube.playlistItems().list( playlistId=uploads_list_id, part='snippet', maxResults=50, pageToken=next_page_token ).execute() for playlist_item in playlistitems_response['items']: videos.append(playlist_item) next_page_token = playlistitems_response.get('tokenPagination', {}).get( 'nextPageToken') if len(videos) > 100: break template_values = { 'channel_name': channel_name, 'videos': videos } self.response.headers['Content-type'] = 'text/html' template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render(template_values)) app = webapp2.WSGIApplication([ ('/.*', MainHandler), ], debug=True) ``` --- # Source: https://developers.google.com/youtube/v3/code_samples/ruby.md.txt # Ruby Code Samples The following code samples, which use the [Google APIs Client Library for Ruby](https://developers.google.com/api-client-library/ruby), are available for the YouTube Data API. You can download these code samples from the `ruby` folder of the [YouTube APIs code sample repository on GitHub](https://github.com/youtube/api-samples). ## Add a channel subscription This sample calls the API's `subscriptions.insert` method to add a subscription to a specified channel. ```ruby #!/usr/bin/ruby require 'rubygems' gem 'google-api-client', '>0.7' require 'google/api_client' require 'google/api_client/client_secrets' require 'google/api_client/auth/file_storage' require 'google/api_client/auth/installed_app' require 'trollop' # This OAuth 2.0 access scope allows for full read/write access to the # authenticated user's account. YOUTUBE_SCOPE = 'https://www.googleapis.com/auth/youtube' YOUTUBE_API_SERVICE_NAME = 'youtube' YOUTUBE_API_VERSION = 'v3' def get_authenticated_service client = Google::APIClient.new( :application_name => $PROGRAM_NAME, :application_version => '1.0.0' ) youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION) file_storage = Google::APIClient::FileStorage.new("#{$PROGRAM_NAME}-oauth2.json") if file_storage.authorization.nil? client_secrets = Google::APIClient::ClientSecrets.load flow = Google::APIClient::InstalledAppFlow.new( :client_id => client_secrets.client_id, :client_secret => client_secrets.client_secret, :scope => [YOUTUBE_SCOPE] ) client.authorization = flow.authorize(file_storage) else client.authorization = file_storage.authorization end return client, youtube end def main opts = Trollop::options do opt :channel_id, 'ID of the channel to subscribe to.', :type => String, :default => 'UCtVd0c0tGXuTSbU5d8cSBUg' end client, youtube = get_authenticated_service begin body = { :snippet => { :resourceId => { :channelId => opts[:channel_id] } } } # Call the API's youtube.subscriptions.insert method to add the subscription # to the specified channel. subscriptions_response = client.execute!( :api_method => youtube.subscriptions.insert, :parameters => { :part => body.keys.join(',') }, :body_object => body ) puts "A subscription to '#{subscriptions_response.data.snippet.title}' was added." rescue Google::APIClient::TransmissionError => e puts e.result.body end end main https://github.com/youtube/api-samples/blob/07263305b59a7c3275bc7e925f9ce6cabf774022/ruby/add_subscription.rb ``` ## Authorize a request The following code sample performs OAuth 2.0 authorization by checking for the presence of a local file that contains authorization credentials. If the file is not present, the script opens a browser and waits for a response, then saves the returned credentials locally. ```ruby require 'google/api_client' require 'google/api_client/client_secrets' require 'json' require 'launchy' require 'thin' RESPONSE_HTML = <