Skip to content

Instantly share code, notes, and snippets.

@anbublacky
Created July 1, 2015 03:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anbublacky/aedee0284f1bd2ea0a71 to your computer and use it in GitHub Desktop.
Save anbublacky/aedee0284f1bd2ea0a71 to your computer and use it in GitHub Desktop.
google api v3, set youtube channer banner, using 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'
# This OAuth 2.0 access scope allows for read-only access to the authenticated
# user's account, but not other types of account access.
YOUTUBE_READ_WRITE_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_READ_WRITE_SCOPE]
)
client.authorization = flow.authorize(file_storage)
else
client.authorization = file_storage.authorization
end
return client, youtube
end
def main
client, youtube = get_authenticated_service
begin
# Retrieve the "contentDetails" part of the channel resource for the
# authenticated user's channel.
body = {
:brandingSettings =>
{
:image =>
{
:bannerImageUrl => "http://www.hdwallpapersinn.com/wp-content/uploads/2014/08/51b451a37900336010.jpg"
}
},
:id => "UCKfVENopqTI7b-pRo5khMKw"
}
image_insert_response = client.execute!(
:api_method => youtube.channel_banners.insert,
:body_object => body,
:media => Google::APIClient::UploadIO.new("wallhaven-109381.jpg", 'image/jpeg'),
:parameters => {
:uploadType => 'multipart',
:part => "brandingSettings"
}
)
channel_list = client.execute!(
:api_method => youtube.channels.list,
:parameters => {
:mine => true,
:part => "brandingSettings"
})
channel_list_res = JSON.parse(channel_list.response.body)
banner_url = JSON.parse(image_insert_response.body)["url"]
channel_list_res["items"][0]["brandingSettings"]["image"]["bannerExternalUrl"] = []
channel_brandingSettings = channel_list_res["items"][0]["brandingSettings"]
channel_brandingSettings["image"]["bannerExternalUrl"] = banner_url
puts channel_list_res
channels_update_response = client.execute!(
:api_method => youtube.channels.update,
:parameters => {
:part => 'brandingSettings',
},
:body_object => {
'brandingSettings' => channel_brandingSettings,
'id' => channel_list_res['items'][0]['id']
})
rescue Google::APIClient::TransmissionError => e
puts e.result.body
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment