Page Summary
-
This webpage provides code samples for the YouTube Data API using the Google APIs Client Library for Ruby, downloadable from the
rubyfolder in the YouTube APIs code sample repository on GitHub. -
The samples include functionality for adding a channel subscription, which is done by calling the
subscriptions.insertmethod. -
OAuth 2.0 authorization is demonstrated through a code sample that checks for a local file with credentials, and if not present, opens a browser to get them and save them for future use.
-
Another sample code allows users to retrieve videos that have been uploaded to their channel by using the
playlistItems.listmethod and thechannels.listmethod. -
Users can use a code sample that utilizes the
search.listmethod to search YouTube for specific keywords, and it will retrieve videos, channels, and playlists related to the searched term. -
A sample is available that shows how to upload a video to a YouTube channel using the
videos.insertmethod.
The following code samples, which use the Google APIs Client Library for 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.
Add a channel subscription
This sample calls the API's subscriptions.insert method to add a subscription
to a specified channel.
#!/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