Skip to content

Commit

Permalink
"Updating samples to reflect recent changes."
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick committed Feb 12, 2014
1 parent 327b374 commit 9e104df
Showing 1 changed file with 58 additions and 31 deletions.
89 changes: 58 additions & 31 deletions ruby/add_subscription.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,73 @@
#!/usr/bin/ruby

require 'rubygems'
gem 'google-api-client', '>0.7'
require 'google/api_client'
# The oauth/oauth_util code is not part of the official Ruby client library.
# Download it from:
# http://samples.google-api-ruby-client.googlecode.com/git/oauth/oauth_util.rb
require 'oauth/oauth_util'

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'

client = Google::APIClient.new
youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
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

auth_util = CommandLineOAuthHelper.new(YOUTUBE_SCOPE)
client.authorization = auth_util.authorize()
client, youtube = get_authenticated_service

body = {
:snippet => {
:resourceId => {
:kind => 'youtube#channel',
# Replace the value below with the ID of the channel being subscribed to.
:channelId => 'UCtVd0c0tGXuTSbU5d8cSBUg'
begin
body = {
:snippet => {
:resourceId => {
:channelId => opts[:channel_id]
}
}
}
}
}

# Call the API's youtube.subscriptions.insert method to add the subscription
# to the specified channel.
begin
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::ClientError => e
puts "#{e}: Unable to add subscription. Are you already subscribed?"

# 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

0 comments on commit 9e104df

Please sign in to comment.