Skip to content

Instantly share code, notes, and snippets.

@claudiob
Last active August 29, 2015 14:02
Show Gist options
  • Save claudiob/f775b5d988c80e24169f to your computer and use it in GitHub Desktop.
Save claudiob/f775b5d988c80e24169f to your computer and use it in GitHub Desktop.
How to like a YouTube video in Ruby
def like(video_id, token)
request = `curl -X POST -H 'Content-length: 0' -I -H 'Authorization: Bearer #{token}'
'https://www.googleapis.com/youtube/v3/videos/rate?id=#{video_id}&rating=like'`
request =~ %r{^HTTP/1.1 204}
end
# gem install google_api_client
require 'google/api_client'
def like(video_id, token)
client = Google::APIClient.new application_name: 'YourApp', application_version: '1.0'
client.authorization.access_token = token
yt = client.discovered_api 'youtube', 'v3'
response = client.execute! api_method : yt.videos.rate, parameters: {id: video_id, rating: "like"}
response.status == 204
end
# gem install youtube_it
require 'youtube_it'
def like(video_id, token, dev_key)
client = YouTubeIt::OAuth2Client.new client_access_token: token, dev_key: dev_key
response = client.like_video video_id
response[:code] == 204
end
# gem install googol
require 'googol'
def like(video_id, token)
account = Googol::YoutubeAccount.new access_token: token
account.like! video_id: video_id
end
# gem install yt
require 'yt'
def like(video_id, token)
account = Yt::Account.new access_token: token
video = Yt::Video.new id: video_id, auth: account
video.like # also: video.liked?, video.dislike, ...
end
# from https://github.com/Fullscreen/yt/blob/master/lib/yt/models/video.rb
class Yt::Models::Video
has_many :annotations
has_one :rating
def liked? ... end
def like ... end
def dislike ... end
def unlike ... end
end
# from https://github.com/kylejginavan/youtube_it/blob/master/lib/youtube_it/client.rb
class YouTubeIt::Client
def videos_by(params, options={}) ... end
def video_by(video) ... end
def video_by_user(user, vid) ... end
def video_partial_update(video_id, opts = {}) ... end
def add_comment(video_id, comment, opts = {}) ... end
def profile(user = nil) ... end
def activity(user = nil, opts = {}) ... end
def watchlater(user = nil) ... end
def playlist(playlist_id, opts = {}) ... end
def playlists(user = nil, opts = nil) ... end
def current_user ... end
# ... 50 more methods ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment