Class: YouTubeIt::OAuthClient

Inherits:
Client
  • Object
show all
Defined in:
lib/youtube_it/client.rb

Instance Method Summary collapse

Methods inherited from Client

#add_comment, #add_favorite, #add_playlist, #add_video_to_playlist, #comments, #delete_favorite, #delete_playlist, #delete_video_from_playlist, #dislike_video, #enable_http_debugging, #favorites, #like_video, #my_video, #my_videos, #playlist, #playlists, #profile, #subscribe_channel, #subscriptions, #unsubscribe_channel, #update_playlist, #upload_token, #video_by, #video_by_user, #video_delete, #video_update, #video_upload, #videos_by

Methods included from Logging

#logger

Constructor Details

#initialize(*params) ⇒ OAuthClient

Returns a new instance of OAuthClient.



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/youtube_it/client.rb', line 292

def initialize *params
  if params.first.is_a?(Hash)
    hash_options = params.first
    @consumer_key                  = hash_options[:consumer_key]
    @consumer_secret               = hash_options[:consumer_secret]
    @user                          = hash_options[:username]
    @dev_key                       = hash_options[:dev_key]
    @client_id                     = hash_options[:client_id] || "youtube_it"
    @legacy_debug_flag             = hash_options[:debug]
  else
    puts "* warning: the method YouTubeIt::OAuthClient.new(consumer_key, consumer_secrect, dev_key) is depricated, use YouTubeIt::OAuthClient.new(:consumer_key => 'consumer key', :consumer_secret => 'consumer secret', :dev_key => 'dev_key')"
    @consumer_key                  = params.shift
    @consumer_secret               = params.shift
    @dev_key                       = params.shift
    @user                          = params.shift
    @client_id                     = params.shift || "youtube_it"
    @legacy_debug_flag             = params.shift
  end
end

Instance Method Details

#access_tokenObject



324
325
326
# File 'lib/youtube_it/client.rb', line 324

def access_token
  @access_token = ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
end

#authorize_from_access(atoken, asecret) ⇒ Object



343
344
345
# File 'lib/youtube_it/client.rb', line 343

def authorize_from_access(atoken,asecret)
  @atoken,@asecret = atoken, asecret
end

#authorize_from_request(rtoken, rsecret, verifier) ⇒ Object



337
338
339
340
341
# File 'lib/youtube_it/client.rb', line 337

def authorize_from_request(rtoken,rsecret,verifier)
  request_token = ::OAuth::RequestToken.new(consumer,rtoken,rsecret)
  access_token = request_token.get_access_token({:oauth_verifier => verifier})
  @atoken,@asecret = access_token.token, access_token.secret
end

#config_tokenObject



328
329
330
331
332
333
334
335
# File 'lib/youtube_it/client.rb', line 328

def config_token
  {
    :consumer_key => @consumer_key,
    :consumer_secret => @consumer_secret,
    :token => @atoken,
    :token_secret => @asecret
   }
end

#consumerObject



312
313
314
315
316
317
318
# File 'lib/youtube_it/client.rb', line 312

def consumer
  @consumer ||= ::OAuth::Consumer.new(@consumer_key,@consumer_secret,{
    :site=>"https://www.google.com",
    :request_token_path=>"/accounts/OAuthGetRequestToken",
    :authorize_path=>"/accounts/OAuthAuthorizeToken",
    :access_token_path=>"/accounts/OAuthGetAccessToken"})
end

#current_userObject



347
348
349
350
351
352
353
354
355
356
# File 'lib/youtube_it/client.rb', line 347

def current_user
  yt_session = Faraday.new(:url => "http://gdata.youtube.com") do |builder|
    builder.use Faraday::Response::YouTubeIt 
    builder.use Faraday::Request::OAuth, config_token
    builder.adapter Faraday.default_adapter          
  end
  
  body = yt_session.get("/feeds/api/users/default").body
  REXML::Document.new(body).elements["entry"].elements['author'].elements['name'].text
end

#request_token(callback) ⇒ Object



320
321
322
# File 'lib/youtube_it/client.rb', line 320

def request_token(callback)
  @request_token = consumer.get_request_token({:oauth_callback => callback},{:scope => "http://gdata.youtube.com"})
end