Class: YouTubeIt::OAuthClient

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

Instance Method Summary collapse

Methods inherited from Client

#activity, #add_comment, #add_favorite, #add_playlist, #add_response, #add_video_to_playlist, #comments, #delete_favorite, #delete_playlist, #delete_response, #delete_video_from_playlist, #dislike_video, #enable_http_debugging, #favorites, #like_video, #message_delete, #my_contacts, #my_messages, #my_video, #my_videos, #new_subscription_videos, #playlist, #playlists, #profile, #send_message, #subscribe_channel, #subscriptions, #unsubscribe_channel, #update_playlist, #upload_token, #video_by, #video_by_user, #video_delete, #video_update, #video_upload, #videos_by, #watch_history

Methods included from Logging

#logger

Constructor Details

#initialize(*params) ⇒ OAuthClient

Returns a new instance of OAuthClient.



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/youtube_it/client.rb', line 341

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



373
374
375
# File 'lib/youtube_it/client.rb', line 373

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

#authorize_from_access(atoken, asecret) ⇒ Object



392
393
394
# File 'lib/youtube_it/client.rb', line 392

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

#authorize_from_request(rtoken, rsecret, verifier) ⇒ Object



386
387
388
389
390
# File 'lib/youtube_it/client.rb', line 386

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



377
378
379
380
381
382
383
384
# File 'lib/youtube_it/client.rb', line 377

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

#consumerObject



361
362
363
364
365
366
367
# File 'lib/youtube_it/client.rb', line 361

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



396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/youtube_it/client.rb', line 396

def current_user
  profile = access_token.get("http://gdata.youtube.com/feeds/api/users/default")
  response_code = profile.code.to_i

  if response_code/10 == 20 # success
    REXML::Document.new(profile.body).elements["entry"].elements['author'].elements['name'].text
  elsif response_code == 403 || response_code == 401 # auth failure
    raise YouTubeIt::Upload::AuthenticationError.new(profile.inspect, response_code)
  else
    raise YouTubeIt::Upload::UploadError.new(profile.inspect, response_code)
  end
end

#request_token(callback) ⇒ Object



369
370
371
# File 'lib/youtube_it/client.rb', line 369

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