You're viewing a comment by Elephant and its responses.

Elephant Permalink
November 17, 2009, 16:55

I made the script below in a rush, so it's not the fix of the original code but something that just works (it does upload the video but doesn't check for successful login, nor fills the separate form for title and such, sorry!), tested only under Linux on 17th Nov 2009. Can maybe someone with more time update the original code as well?

Note that the reason I needed this script is because my Internet connection isn't fast and stable, and I had to upload my video with various tricks; this script is one of those. I have no intention of abusing it and you shouldn't either.

Regarding the YouTube API: I think it was designed for different purposes, correct me if I'm wrong. I find it hard to grasp, too.


use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use Net::SSLeay;


my $username = 'username';
my $password = 'password';
my $file = './test.avi';


# TODO: $HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1;

my $loginPage = '' .
    '' .

my $loginForm = '';
my $uploadPage = '';
my $uploadForm = '';

my $GALX;

sub findGALX
	$GALX = $_[2] if $_[1] eq "GALX";

my $ua = LWP::UserAgent->new
	cookie_jar		=> {},
	agent			=> 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv: Gecko/20090824 Firefox/3.5.3'

push @{$ua->requests_redirectable}, 'POST';


print "\n--- Requesting login page...\n";

my $resp = $ua->get($loginPage);

unless ($resp->is_success)
	die "Unable to get login page: ", $resp->status_line;

print "\n--- Logging in...\n";


my $req = POST $loginForm,
	'ltmpl'				=> 'sso',
	'continue'			=> '',
	'service'			=> 'youtube',
	'uilel'				=> '3',
	'hl'				=> 'en_US',
	'GALX'				=> $GALX,
	'Email'				=> $username,
	'Passwd'			=> $password,
	'PersistentCookie'	=> 'yes',
	'rmShown'			=> '1',
	'signIn'			=> 'Sign in',
	'asts'				=> ''

$resp = $ua->request($req);

unless ($resp->is_success)
	die "Couldn't log in: ", $resp->status_line;

print "\n--- Requesting the page after login...\n";

my ($nextPage) = $resp->header('Refresh') =~ /0; url='([^']+)'/;

print $nextPage . "\n";

my $req = GET $nextPage;
$resp = $ua->request($req);

print "\n--- Requesting upload page...\n";

my $req = GET $uploadPage;
$resp = $ua->request($req);

print "\n--- Uploading...\n";

my ($uploadKey) = $resp->content =~ /yt\.www\.upload\.UploadSettings\.Keys\.UPLOAD_KEY, "([^"]+)"/m;
my ($sessionKey) = $resp->content =~ /yt\.www\.upload\.UploadSettings\.Keys\.SESSION_KEY, "([^"]+)"/m;
my ($sessionToken) = $resp->content =~ /'XSRF_TOKEN': '([^']+)'/m;

my $req = POST $uploadForm,
	Content_Type	=> 'form-data',
	Content			=> [
		'uploader_type' => 'Web_HTML',
		'return_address' => '',
		'upload_key' => $uploadKey,
		'action_postvideo' => '1',
		'live_thumbnail_id' => $sessionKey,
		'up_locale' => 'en_US',
		'up_content_region' => 'US',
		'up_country' => 'US',
		'field_uploadfile' => [ $file ],
		'session_token' => $sessionToken,

$resp = $ua->request($req);

# open (MYFILE, '>content.html') || die $!;
# print MYFILE $resp->content . "\n";
# close MYFILE;

Comment Responses

Senthil Murugan Permalink
February 23, 2013, 18:27

this code is not working for me,i changed all details in that file.but
the error is $uploadForm url is not found.then how i change that video upload url..?plz give me a solution or give any other code for upload a video to Youtube..i need urgently.plz...

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "browser": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.