Skip to content

Instantly share code, notes, and snippets.

@anbublacky
Created June 30, 2015 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anbublacky/ff76b56642613bfe3066 to your computer and use it in GitHub Desktop.
Save anbublacky/ff76b56642613bfe3066 to your computer and use it in GitHub Desktop.
youtube channel upload image
#!/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)
puts channel_list_res
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