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, #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

Methods included from Logging

#logger

Constructor Details

#initialize(options) ⇒ OAuth2Client

Returns a new instance of OAuth2Client.



409
410
411
412
413
414
415
# File 'lib/youtube_it/client.rb', line 409

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]
  @dev_key              = options[:dev_key]
end

Instance Method Details

#access_tokenObject



424
425
426
# File 'lib/youtube_it/client.rb', line 424

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

#current_userObject



432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/youtube_it/client.rb', line 432

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



417
418
419
420
421
422
# File 'lib/youtube_it/client.rb', line 417

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



428
429
430
# File 'lib/youtube_it/client.rb', line 428

def refresh_access_token!
  @access_token = access_token.refresh!
end