Permalink
Browse files

Merge branch 'master' of github.com:kylejginavan/youtube_it

  • Loading branch information...
2 parents 15cae4e + 77cfdf6 commit 5c719aae43d9eb908a76644f5850d22ed38ddbff @kylejginavan kylejginavan committed Nov 28, 2010
Showing with 112 additions and 17 deletions.
  1. +77 −8 lib/youtube_it/client.rb
  2. +35 −9 lib/youtube_it/request/video_upload.rb
@@ -166,7 +166,7 @@ def enable_http_debugging
private
def client
- @client ||= YouTubeIt::Upload::VideoUpload.new(@user, @pass, @dev_key)
+ @client ||= YouTubeIt::Upload::VideoUpload.new(:username => @user, :password => @pass, :dev_key => @dev_key)
end
def calculate_offset(page, per_page)
@@ -179,6 +179,74 @@ def integer_or_default(value, default)
end
end
+ class AuthSubClient < Client
+ def initialize *params
+ if params.first.is_a?(Hash)
+ hash_options = params.first
+ @authsub_token = hash_options[:token]
+ @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::AuthSubClient.new(token, dev_key) is depricated, use YouTubeIt::AuthSubClient.new(:token => 'token', :dev_key => 'dev_key')"
+ @authsub_token = params.shift
+ @dev_key = params.shift
+ @client_id = params.shift || "youtube_it"
+ @legacy_debug_flag = params.shift
+ end
+ end
+
+ def create_session_token
+ response = nil
+ session_token_url = "/accounts/AuthSubSessionToken"
+
+ http_connection do |session|
+ response = session.get2('https://%s' % session_token_url,session_token_header).body
+ end
+ @authsub_token = response.sub('Token=','')
+ end
+
+ def revoke_session_token
+ response = nil
+ session_token_url = "/accounts/AuthSubRevokeToken"
+
+ http_connection do |session|
+ response = session.get2('https://%s' % session_token_url,session_token_header).code
+ end
+ response.to_s == '200' ? true : false
+ end
+
+ def session_token_info
+ response = nil
+ session_token_url = "/accounts/AuthSubTokenInfo"
+
+ http_connection do |session|
+ response = session.get2('https://%s' % session_token_url,session_token_header)
+ end
+ {:code => response.code, :body => response.body }
+ end
+
+ private
+ def client
+ @client ||= YouTubeIt::Upload::VideoUpload.new(:dev_key => @dev_key, :authsub_token => @authsub_token)
+ end
+
+ def session_token_header
+ {
+ "Content-Type" => "application/x-www-form-urlencoded",
+ "Authorization" => "AuthSub token=#{@authsub_token}"
+ }
+ end
+
+ def http_connection
+ http = Net::HTTP.new("www.google.com")
+ http.set_debug_output(logger) if @http_debugging
+ http.start do |session|
+ yield(session)
+ end
+ end
+ end
+
class OAuthClient < Client
def initialize *params
if params.first.is_a?(Hash)
@@ -191,12 +259,12 @@ def initialize *params
@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
+ @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
@@ -231,11 +299,12 @@ def current_user
REXML::Document.new(body).elements["entry"].elements['author'].elements['name'].text
end
+
private
def client
# IMPORTANT: make sure authorize_from_access is called before client is fetched
- @client ||= YouTubeIt::Upload::VideoUpload.new(@user, "", @dev_key, "youtube_it", access_token)
+ @client ||= YouTubeIt::Upload::VideoUpload.new(:username => current_user, :dev_key => @dev_key, :acces_token => access_token)
end
end
@@ -16,11 +16,32 @@ class AuthenticationError < YouTubeIt::Error; end
#
class VideoUpload
include YouTubeIt::Logging
- def initialize user, pass, dev_key, client_id = 'youtube_it', access_token = nil
- @user, @pass, @dev_key, @client_id, @access_token = user, pass, dev_key, client_id, access_token
+ def initialize user, pass, dev_key, access_token = nil, authsub_token = nil, client_id = 'youtube_it'
+ @user, @password, @dev_key, @access_token, @authsub_token, @client_id = user, pass, dev_key, access_token, authsub_token, client_id
@http_debugging = false
end
+ def initialize *params
+ if params.first.is_a?(Hash)
+ hash_options = params.first
+ @user = hash_options[:username]
+ @password = hash_options[:password]
+ @dev_key = hash_options[:dev_key]
+ @access_token = hash_options[:access_token]
+ @authsub_token = hash_options[:authsub_token]
+ @client_id = hash_options[:client_id] || "youtube_it"
+ else
+ puts "* warning: the method YouTubeIt::Upload::VideoUpload.new(username, password, dev_key) is depricated, use YouTubeIt::Upload::VideoUpload.new(:username => 'user', :password => 'passwd', :dev_key => 'dev_key')"
+ @user = params.shift
+ @password = params.shift
+ @dev_key = params.shift
+ @access_token = params.shift
+ @authsub_token = params.shift
+ @client_id = params.shift || "youtube_it"
+ end
+ end
+
+
def enable_http_debugging
@http_debugging = true
end
@@ -396,12 +417,17 @@ def authorization_headers_for_oauth
end
def authorization_headers
- {
- "Authorization" => "GoogleLogin auth=#{auth_token}",
- "X-GData-Client" => "#{@client_id}",
- "X-GData-Key" => "key=#{@dev_key}",
- "GData-Version" => "2",
- }
+ header = {
+ "X-GData-Client" => "#{@client_id}",
+ "X-GData-Key" => "key=#{@dev_key}",
+ "GData-Version" => "2",
+ }
+ if @authsub_token
+ header.merge!("Authorization" => "AuthSub token=#{@authsub_token}")
+ else
+ header.merge!("Authorization" => "GoogleLogin auth=#{auth_token}")
+ end
+ header
end
def parse_upload_error_from(string)
@@ -457,7 +483,7 @@ def auth_token
@auth_token ||= begin
http = Net::HTTP.new("www.google.com", 443)
http.use_ssl = true
- body = "Email=#{YouTubeIt.esc @user}&Passwd=#{YouTubeIt.esc @pass}&service=youtube&source=#{YouTubeIt.esc @client_id}"
+ body = "Email=#{YouTubeIt.esc @user}&Passwd=#{YouTubeIt.esc @password}&service=youtube&source=#{YouTubeIt.esc @client_id}"
response = http.post("/youtube/accounts/ClientLogin", body, "Content-Type" => "application/x-www-form-urlencoded")
raise UploadError, response.body[/Error=(.+)/,1] if response.code.to_i != 200
@auth_token = response.body[/Auth=(.+)/, 1]

0 comments on commit 5c719aa

Please sign in to comment.