Class: YTAnalytics::OAuth2Client

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

Instance Method Summary collapse

Methods inherited from Client

#analytics, #day_totals, #enable_http_debugging, #month_totals, #my_video, #my_videos, #seven_day_totals, #thirty_day_totals, #watch_history

Methods included from Logging

#logger

Constructor Details

#initialize(options) ⇒ OAuth2Client

Returns a new instance of OAuth2Client.



84
85
86
87
88
89
90
91
92
# File 'lib/yt_analytics/client.rb', line 84

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



103
104
105
# File 'lib/yt_analytics/client.rb', line 103

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



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/yt_analytics/client.rb', line 122

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
    Nokogiri::XML(profile.body).at("entry/author/name").text
  elsif response_code == 403 || response_code == 401 # auth failure
    raise YTAnalytics::YTAuth::AuthenticationError.new(profile.inspect, response_code)
  else
    raise YTAnalytics::YTAuth::UploadError.new(profile.inspect, response_code)
  end
end

#oauth_clientObject



94
95
96
97
98
99
100
101
# File 'lib/yt_analytics/client.rb', line 94

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



107
108
109
110
111
112
113
114
115
# File 'lib/yt_analytics/client.rb', line 107

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



117
118
119
120
# File 'lib/yt_analytics/client.rb', line 117

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

#user_idObject



135
136
137
138
# File 'lib/yt_analytics/client.rb', line 135

def user_id
  profile ||= access_token.get("http://gdata.youtube.com/feeds/api/users/default?v=2&alt=json").parsed
  profile['entry']['author'][0]["yt$userId"]["$t"]
end