Class: YouTubeIt::OAuth2Client

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(options) ⇒ OAuth2Client

Returns a new instance of OAuth2Client.



419
420
421
422
423
424
425
426
427
# File 'lib/youtube_it/client.rb', line 419

def initialize(options)
  @client_id               = options[:client_id]
  @client_secret           = options[:client_secret]
  @client_access_token     = options[:client_access_token]
  @client_refresh_token    = options[:client_refresh_token]
  @client_token_expires_at = options[:client_token_expires_at]
  @dev_key                 = options[:dev_key]
  @legacy_debug_flag       = options[:debug]
end

Instance Method Details

#access_tokenObject



436
437
438
# File 'lib/youtube_it/client.rb', line 436

def access_token
  @access_token ||= ::OAuth2::AccessToken.new(oauth_client, @client_access_token, :refresh_token => @client_refresh_token, :expires_at => @client_token_expires_at)
end

#current_userObject



450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/youtube_it/client.rb', line 450

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

  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

#oauth_clientObject



429
430
431
432
433
434
# File 'lib/youtube_it/client.rb', line 429

def oauth_client
  @oauth_client ||= ::OAuth2::Client.new(@client_id, @client_secret,
                                         :site => "https://accounts.google.com",
                                         :authorize_url => '/o/oauth2/auth',
                                         :token_url => '/o/oauth2/token')
end

#refresh_access_token!Object



440
441
442
443
444
445
446
447
448
# File 'lib/youtube_it/client.rb', line 440

def refresh_access_token!
  new_access_token = access_token.refresh!
  require 'thread' unless Thread.respond_to?(:exclusive)
  Thread.exclusive do
    @access_token = new_access_token
    @client = nil
  end
  @access_token
end