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, #add_video_to_watchlater, #all_playlists, #captions_update, #comments, #delete_comment, #delete_favorite, #delete_playlist, #delete_response, #delete_video_from_playlist, #delete_video_from_watchlater, #dislike_video, #enable_http_debugging, #favorites, #get_all_videos, #like_video, #message_delete, #my_contacts, #my_messages, #my_video, #my_videos, #new_subscription_videos, #playlist, #playlists, #profile, #profiles, #send_message, #subscribe_channel, #subscriptions, #unsubscribe_channel, #update_playlist, #update_position_video_from_playlist, #upload_token, #video_by, #video_by_user, #video_delete, #video_partial_update, #video_update, #video_upload, #videos, #videos_by, #watch_history, #watchlater

Methods included from Logging

#logger

Constructor Details

#initialize(options) ⇒ OAuth2Client

Returns a new instance of OAuth2Client.



484
485
486
487
488
489
490
491
492
# File 'lib/youtube_it/client.rb', line 484

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



503
504
505
# File 'lib/youtube_it/client.rb', line 503

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



522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/youtube_it/client.rb', line 522

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

  if (response_code / 10).to_i == 20 # success
    Nokogiri::XML(profile.body).at("//yt:username").text
  elsif response_code == 403 || response_code == 401 # auth failure
    raise AuthenticationError.new(profile.inspect, response_code)
  else
    raise UploadError.new(profile.inspect, response_code)
  end
end

#oauth_clientObject



494
495
496
497
498
499
500
501
# File 'lib/youtube_it/client.rb', line 494

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

#refresh_access_token!Object



507
508
509
510
511
512
513
514
515
# File 'lib/youtube_it/client.rb', line 507

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

#session_token_infoObject



517
518
519
520
# File 'lib/youtube_it/client.rb', line 517

def session_token_info
  response = Faraday.get("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=#{@client_access_token}")
  {:code => response.status, :body => response.body }
end