Jump to content

[SOLVED] login at youtube using cURL and php ?


xt3mp0r~

Recommended Posts

Hello. I'm quite a newbie to cURL and php. I have been trying hard to login at youtube using cURL but haven't had any success.  :-\

 

All i get in return when i run the following code is the youtube login page, with my username(which i used in the code) written at the username Field and it doesn't login.  >:(

 

Can anyone tell me what's goin on ?  :confused:

 

<?php

$posturl  = '&GALX=obiRBhLUBIa&Email=myfancyusername@gmail.com&Passwd=myfancypassword&rmShown=1&signIn=Sign+in&asts=';

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieYoutube");
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/accounts/ServiceLogin?service=youtube");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieYoutube");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_REFERER,"https://www.google.com/accounts/ServiceLogin?service=youtube");
curl_setopt($ch, CURLOPT_POSTFIELDS, $posturl);


$output = curl_exec ($ch); 

curl_close($ch);

echo $output;

?>

Link to comment
Share on other sites

I was just working on the same script last night when I came across your post. I'm new to PHP as well so excuse the coding style ;)

 

I finally managed to complete it this morning so here's the completed copy.

 

<?php
$username = 'XXXXXXXXXX';
$password = 'XXXXXXXXXX';
$cookie_jar_path = "cookies/cookie.txt";
$agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.13) Gecko/2009080315 Ubuntu/9.04 (jaunty) Firefox/3.0.13";

$youtube_login_form = YouTubeLoginPage($cookie_jar_path, $agent); 
$LoggedInPage = SendYouTubeLogin($youtube_login_form, $username, $password, $cookie_jar_path, $agent);
print_r($LoggedInPage); 

function SendYouTubeLogin($youtube_form, $username, $password, $cookie_jar_path, $agent) {

$referer = "https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http://www.youtube.com/signin?action_handle_signin=true&nomobiletemp=1&hl=en_US&next=%2Findex&hl=en_US&ltmpl=sso";

//Grab Form Code
$pattern = '/(?s)name="GALX"(.*?)value="(.*?)"/';
preg_match($pattern, $youtube_form, $matches); 
$galx = $matches[2];

$post = "ltmpl=sso&continue=";
$post .= urlencode("http://www.youtube.com/signin?action_handle_signin=true&nomobiletemp=1&hl=en_US&next=/");
$post .= "index&service=youtube&uilel=3&ltmpl=sso&hl=en_US&ltmpl=sso&GALX=$galx&Email=$username&Passwd=$password&PersistentCookie=yes&rmShown=1&signIn=Sign in&asts=";

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL,"https://www.google.com/accounts/ServiceLoginAuth?service=youtube");
       curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
       curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar_path);
       curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar_path);
       curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt ($ch, CURLOPT_POSTFIELDS, $post); 
       curl_setopt ($ch, CURLOPT_POST, 1); 
       curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
        
       $syn = curl_exec($ch);
        curl_close($ch);
        
        return $syn;
}

function YouTubeLoginPage($cookie_jar_path, $agent) {

$url = "https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http://www.youtube.com/signin?action_handle_signin=true&nomobiletemp=1&hl=en_US&next=%2Findex&hl=en_US&ltmpl=sso";
$referer = "http://www.youtube.com/";
           
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_REFERER, $referer); 
       curl_setopt ($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar_path);
       curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar_path);
       curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 30);

        $curl_output = curl_exec($ch);
        curl_close($ch);
        $pattern = '/(?s)\<form(.*?)\<\/form\>/';
        preg_match_all($pattern,  $curl_output , $matches);
        
        $youtube_form = $matches[0][1];
       
        return $youtube_form;

}

?>

 

The key parts  I noticed missing from your script is:

1) the value name GALX rom the form which changes every time you hit the page. I used preg_match and preg_match_all to extract that portion of code. If your skilled enough with regex (I'm not) you'll be able to grab that code in one statement rather then two.

 

2) Performing two curl steps before you login. The two step approach sets the cookie on the initial page load, then the login checks to see if the cookie is set.

 

Here's an EBAY example from the curl page I was basing my script from: http://curl.haxx.se/libcurl/php/examples/

 

ALSO make sure your cookie file and directory exists and is writable.Running this from the command line with verbose turned on will show if it isn't.

 

I tried to clean up the code as best I could so if you have any questions about it feel free to ask.

 

Good luck :)

Link to comment
Share on other sites

I was just working on the same script last night when I came across your post. I'm new to PHP as well so excuse the coding style ;)

 

I finally managed to complete it this morning so here's the completed copy.

 

The key parts  I noticed missing from your script is:

1) the value name GALX rom the form which changes every time you hit the page. I used preg_match and preg_match_all to extract that portion of code. If your skilled enough with regex (I'm not) you'll be able to grab that code in one statement rather then two.

 

2) Performing two curl steps before you login. The two step approach sets the cookie on the initial page load, then the login checks to see if the cookie is set.

 

Here's an EBAY example from the curl page I was basing my script from: http://curl.haxx.se/libcurl/php/examples/

 

ALSO make sure your cookie file and directory exists and is writable.Running this from the command line with verbose turned on will show if it isn't.

 

I tried to clean up the code as best I could so if you have any questions about it feel free to ask.

 

Good luck :)

 

That is a very nifty script..

 

Just wondering, what could be the use of it?

 

You go to your website and it logs you into youtube and redirects you to the youtube homepage.. Not very much point in that (In my opinion)

 

Could somebody mention a use for it?

Link to comment
Share on other sites

I was just working on the same script last night when I came across your post. I'm new to PHP as well so excuse the coding style ;)

 

I finally managed to complete it this morning so here's the completed copy.

 

The key parts  I noticed missing from your script is:

1) the value name GALX rom the form which changes every time you hit the page. I used preg_match and preg_match_all to extract that portion of code. If your skilled enough with regex (I'm not) you'll be able to grab that code in one statement rather then two.

 

2) Performing two curl steps before you login. The two step approach sets the cookie on the initial page load, then the login checks to see if the cookie is set.

 

Here's an EBAY example from the curl page I was basing my script from: http://curl.haxx.se/libcurl/php/examples/

 

ALSO make sure your cookie file and directory exists and is writable.Running this from the command line with verbose turned on will show if it isn't.

 

I tried to clean up the code as best I could so if you have any questions about it feel free to ask.

 

Good luck :)

 

That is a very nifty script..

 

Just wondering, what could be the use of it?

 

You go to your website and it logs you into youtube and redirects you to the youtube homepage.. Not very much point in that (In my opinion)

 

Could somebody mention a use for it?

 

Thx :)

 

It's the beginning portion of a bigger script and a template people could use for other similar logins.

Link to comment
Share on other sites

You go to your website and it logs you into youtube and redirects you to the youtube homepage.. Not very much point in that (In my opinion)

 

Could somebody mention a use for it?

 

Probably to comment spam. ::)

 

Automated video posting?

 

Well I have never heard of anyone needed to upload videos with cURL. But usually these things when there automated = spam. Like running a bot with cURL to mass spam hundreds of pages a second or automate the upload of tons of spam videos.  LOL :D

Link to comment
Share on other sites

You go to your website and it logs you into youtube and redirects you to the youtube homepage.. Not very much point in that (In my opinion)

 

Could somebody mention a use for it?

 

Probably to comment spam. ::)

 

Automated video posting?

 

Well I have never heard of anyone needed to upload videos with cURL. But usually these things when there automated = spam. Like running a bot with cURL to mass spam hundreds of pages a second or automate the upload of tons of spam videos.  LOL :D

 

For me that script was a practice for a few other similar logins I'm working on.

 

I'm just giving back to the community. What he uses it for is his problem.

Link to comment
Share on other sites

 

 

The key parts  I noticed missing from your script is:

1) the value name GALX rom the form which changes every time you hit the page. I used preg_match and preg_match_all to extract that portion of code. If your skilled enough with regex (I'm not) you'll be able to grab that code in one statement rather then two.

 

2) Performing two curl steps before you login. The two step approach sets the cookie on the initial page load, then the login checks to see if the cookie is set.

 

Here's an EBAY example from the curl page I was basing my script from: http://curl.haxx.se/libcurl/php/examples/

 

ALSO make sure your cookie file and directory exists and is writable.Running this from the command line with verbose turned on will show if it isn't.

 

I tried to clean up the code as best I could so if you have any questions about it feel free to ask.

 

Good luck :)

 

Thanks a lot man  ;)

I have been working on it, since long now..I'm really new to php, but your script helped me complete the script and now i also understood how you did it :D;D

 

 

Link to comment
Share on other sites

 

 

The key parts  I noticed missing from your script is:

1) the value name GALX rom the form which changes every time you hit the page. I used preg_match and preg_match_all to extract that portion of code. If your skilled enough with regex (I'm not) you'll be able to grab that code in one statement rather then two.

 

2) Performing two curl steps before you login. The two step approach sets the cookie on the initial page load, then the login checks to see if the cookie is set.

 

Here's an EBAY example from the curl page I was basing my script from: http://curl.haxx.se/libcurl/php/examples/

 

ALSO make sure your cookie file and directory exists and is writable.Running this from the command line with verbose turned on will show if it isn't.

 

I tried to clean up the code as best I could so if you have any questions about it feel free to ask.

 

Good luck :)

 

Thanks a lot man  ;)

I have been working on it, since long now..I'm really new to php, but your script helped me complete the script and now i also understood how you did it :D;D

 

Anytime :)

 

I'm new to PHP as well (6-8 months) so I understand where your coming from. A tool which helped me throughout the process was the LiveHTTPHeaders Firefox Plugin. Although you could run curl in verbose or wireshark to see the entire handshake process as well.

Link to comment
Share on other sites

 

Anytime :)

 

I'm new to PHP as well (6-8 months) so I understand where your coming from. A tool which helped me throughout the process was the LiveHTTPHeaders Firefox Plugin. Although you could run curl in verbose or wireshark to see the entire handshake process as well.

 

I have been using tamper-data add-on on Firefox, and its good too! :D

Anyhow, the main purpose of the script i want to build is that, i have to disable comments on all the videos in my account. Unfortunately, youtube API doesn't have such option. Thus, i have to try out cURL..

 

So my next part is to disable comments on the video's in my account. :)

Hope i can do it :P

 

 

 

Link to comment
Share on other sites

 

Anytime :)

 

I'm new to PHP as well (6-8 months) so I understand where your coming from. A tool which helped me throughout the process was the LiveHTTPHeaders Firefox Plugin. Although you could run curl in verbose or wireshark to see the entire handshake process as well.

 

I have been using tamper-data add-on on Firefox, and its good too! :D

Anyhow, the main purpose of the script i want to build is that, i have to disable comments on all the videos in my account. Unfortunately, youtube API doesn't have such option. Thus, i have to try out cURL..

 

So my next part is to disable comments on the video's in my account. :)

Hope i can do it :P

 

Thanks for that addon. A much cleaner interface then livehttpheaders.  8)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.