473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Login YouTube using C#

Hello,

What I am trying to do, is login YouTube using C# (HTTPWebRequest).

The problem is, I don't really know how to do it.
Is there any URL I could fill using the username and pass and therefore login, or do I have to somehow fill the form manually and than click on login?

I tried catching the login URL, but this is all I got:
GET /signup?hl=en_US&warned=&nomobiletemp=1&next=/index&action_login&lauth=1gk4O8uGbJOKBnchDLMGcdl1S pnfRikkHLZ0VD_02PuTmfqJSBurvQ==

Thanks in advance
Jun 4 '09 #1
16 16581
Markus
6,050 Expert 4TB
Google is your friend :)
Jun 6 '09 #2
Sorry, but that is not what I am trying to do. API is OK, but its against my needs.

I need to login without use of API, just using httpwebrequest.
Jun 12 '09 #3
Markus
6,050 Expert 4TB
@GVNPublic123
Well you need to send POST data to the youtube login page. I'm not sure how you do that in C#. Does C# have a cURL library? If so, that will help you.
Jun 12 '09 #4
According to my google search, there is one but its told to be messy. I guess that curl things can be done in C# as well.

Is there any recommended way I could POST that data (username, pass and than login)?
Jun 12 '09 #5
Markus
6,050 Expert 4TB
@GVNPublic123
Using cURL it would be very easy. I can't recommend a way because I don't use C#. Hopefully someone else will chime in.
Jun 12 '09 #6
hope so...there must be a way
Jun 12 '09 #7
Markus
6,050 Expert 4TB
@GVNPublic123
In the meantime, here's some resources to get you started.
Jun 12 '09 #8
Ok, I just found something interesting:

https://www.google.com/accounts/Serv...ervice=youtube

POST /accounts/ServiceLoginAuth?service=youtube HTTP/1.1
Host: www.google.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 GTB5 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: https://www.google.com/accounts/Serv...6next%3D/index
Cookie: __utma=173272373.1105690668.1227276687.1242737452. 1244276445.17; __utmc=173272373; GA3T=ANGHy1CFtro; GoogleAccountsLocale_session=en; __utma=173272373.1105690668.1227276687.1238227375. 1238438675.12; __utmz=173272373.1238438675.11.2.utmccn=(referral) |utmcsr=google.com|utmcct=/|utmcmd=referral; LSID=groups2.US|mail|pages|s.SI|sierra|writely|you tube|youtubepartnersyn:DQAAAIMAAADm8DQ0psCXVyFJ7O6 jcpVrhiCtP0g03HsoswT-O1WqdhSkA_zH5uhyMISF0JdOFv3kZ_2Mk_2GkoVdUhkXMtWLVw G_q6aZwX_D9HrqJKt-eepJpxnBeATxrYRTnQ5537u5rBR7MhA_uX_C972xfT4bvGwvoG 3AsC2Jsp76U3AldK86ksP66ISpCgNLm5nDUGI; GAUSR=mail|s.SI|writely:gvnpublic@gmail.com; __utmc=173272373; GALX=nQYPV6vAv54; PREF=ID=47b9b9149021b3b1:U=234f9c3e1a77bf4f:TB=2:L D=en:CR=2:TM=1239037183:LM=1243446110:DV=AA:GM=1:S =ZpvNk3xHHHjJPTFx; rememberme=true; adsenseReferralSourceId=aso; adsenseReferralSubId=ww-en-et-gaia; adsenseReferralUrl=www.google.com/accounts/ManageAccount; adsenseReferralUrlQuery=null; SID=DQAAAIEAAABvGELRP2l_usguy7ysl7Vy0bwRQY21fKhmnQ 2GQ_NZRZG9f8BG61WZJtnTtbEAdsMtl_PMrhFT0YnofhLxeeun 444yMOIuhqbSZi_0yHaVrN8APSGkDF79j0wtajalGlDNzZjJU-Pryqm0d2qW0sPyClE8rLKFoXpxW5zCm0P92Q3-hoVJVTF8vJ5u8juKzMM; NID=23=joYanjPxvSjRfBZ9Ozp2w_gXI9oCowZ9vVFO_GK3jZ7 awg4y-dU62tuwID6eRfX9eCJOkR2yixQCQluXNCSyw_JbW_rL76gWG1s dDaSOb79Lj56X4UujeQJ9FHp96R7p; TZ=-120; S=writely=CNDrwzWvciY
Content-Type: application/x-www-form-urlencoded
Content-Length: 287
ltmpl=sso&continue=http%3A%2F%2Fwww.youtube.com%2F signup%3Fhl%3Den_US%26warned%3D%26nomobiletemp%3D1 %26next%3D%2Findex&service=youtube&uilel=3&ltmpl=s so&hl=en_US&ltmpl=sso&GALX=nQYPV6vAv54&Email=GVNPu blic123&Passwd=hiddenonforums&PersistentCookie=yes &rmShown=1&signIn=Sign+in&asts=
HTTP/1.x 200 OK
Set-Cookie: GoogleAccountsLocale_session=en
Set-Cookie: GALX=nQYPV6vAv54;Path=/accounts;Secure
Content-Type: text/html; charset=UTF-8
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: Mon, 01-Jan-1990 00:00:00 GMT
Content-Encoding: gzip
Date: Sat, 13 Jun 2009 07:59:15 GMT
X-Content-Type-Options: nosniff
Content-Length: 4484
Server: GFE/2.0

Specially got interested in this line:

ltmpl=sso&continue=http%3A%2F%2Fwww.youtube.com%2F signup%3Fhl%3Den_US%26warned%3D%26nomobiletemp%3D1 %26next%3D%2Findex&service=youtube&uilel=3&ltmpl=s so&hl=en_US&ltmpl=sso&GALX=nQYPV6vAv54&Email=GVNPu blic123&Passwd=hiddenonforums&PersistentCookie=yes &rmShown=1&signIn=Sign+in&asts=


Now what I have is:

public bool Login(string username, string password, CookieContainer cookies, string useragent)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.youtube.com/login?next=/index");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
request.CookieContainer = cookies;
request.UserAgent = useragent;
request.Timeout = 0x1388;
request.KeepAlive = false;
}

I guess now I have to somehow post the username and password data. But I have no idea how it should look like. I guess I have to write to request stream, but as I said, I have no idea how ( I am 100% new to web based app).
Jun 13 '09 #9
I figured it out, thanks anyways....
Jun 16 '09 #10
Markus
6,050 Expert 4TB
Post your solution for the benefit of others.

Mark.
Jun 16 '09 #11
I didnt manage to work out the cookies yet (only login).
Jun 16 '09 #12
yawar
8
How you figured it out please? Secondly can you please guide how to get the details of what query is required to be sent to login to any service like youtube/yahoo/hotmail etc. I can login to gmail but for that I had searched for the exact query by creating my own server and then I downloaded the gmail login page changed its FORM ACTION to 127.0.0.1:80 then signed up. My server shown the details which were sent.

But how to manage with iframed pages like mail.live.com I can't find out on how to login there and also youtube.

Please guide
Sep 29 '09 #13
WeCi2i
6
I too was looking for the answer to this problem the other day. I was disappointed to see that not even so much as a partially working answer was posted so I figured I'd make a post now that I have figured it out.

The key lies in the fact that the Google sign in generates some random values that you have to pass with the form in order for the whole thing to work right. You will need to set up a cookie container to use for each of the requests as you go through the login steps.

1. First make a get request to the sign in page. Read through the HTML you receive and look for a variable called GALX. This is the random value that you have to pass with the other user name and password variables to the login form.

2. Make a POST to the login form with the user name, password, GALX variable, and any other variables that are set.

3. If the login was successful, you will receive a page with a meta refresh and a javascript refresh. You must extract the page address from the meta refresh. It is not enough to set the number of levels that will be followed in the request because it will not follow the refresh.

4. Make a GET request to the extracted address to complete the login. At this point your cookie container should contain the cookie needed to remain logged in for other actions.

Here is the quick and dirty way to make what I just said happen. Of course you should take the time to clean this up and refine the code but this will make it happen either way:

HttpWebRequest PreRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true& continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Fa ction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl %3Den_US%26next%3D%252F&hl=en_US&ltmpl=sso");
PreRequest.CookieContainer = cookieContainer;
PreRequest.AllowAutoRedirect = true;
HttpWebResponse PreResponse = (HttpWebResponse)PreRequest.GetResponse();
Stream PreResponseStream = PreResponse.GetResponseStream();
PreResponse.Cookies = PreRequest.CookieContainer.GetCookies(PreRequest.R equestUri);
Encoding PreEnc = System.Text.Encoding.UTF8;
StreamReader PreResponseStreamReader = new StreamReader(PreResponse.GetResponseStream(), PreEnc, true);
String PreMyHTML = PreResponseStreamReader.ReadToEnd();

String Pattern = "name=\"GALX\"[\\s]*value=\"([a-zA-Z0-9-_\\.]*)\"";
Match MyMatch = Regex.Match(PreMyHTML, Pattern);
String GALX = MyMatch.Groups[1].ToString();

HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ServiceLoginAuth?service=youtube&ltmpl=sso&continu e=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_h andle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_U S%26next%3D%252F&service=youtube&uilel=3&ltmpl=sso &hl=en_US&ltmpl=sso&GALX=" + GALX + "&Email=YOUREMAILADDRESS/USERNAME%40gmail.com&Passwd=YOURACCOUNTPASSWORD&rm Shown=1&signIn=Sign+in&asts=");
Request.CookieContainer = cookieContainer;
Request.Method = "POST";
Request.Referer = "https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true& continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Fa ction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl %3Den_US%26next%3D%252F&hl=en_US&ltmpl=sso";
Request.MaximumAutomaticRedirections = 50;
Request.AllowAutoRedirect = true;
Request.KeepAlive = true;
Request.ContentLength = 0;

// Get the response
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

Stream ResponseStream = Response.GetResponseStream();
Response.Cookies = Request.CookieContainer.GetCookies(Request.Request Uri);

// Read the response from the stream
Encoding Enc = System.Text.Encoding.UTF8;
StreamReader ResponseStreamReader = new StreamReader(Response.GetResponseStream(), Enc, true);

String MyHTML = ResponseStreamReader.ReadToEnd();
//textBox2.Text = MyHTML;

String Pattern2 = "location\\.replace\\(\"(http:\\/\\/www\\.youtube\\.com\\/signin[a-zA-Z0-9\\\\\\?\\%_-]*)\"\\)";
Match MyMatch2 = Regex.Match(MyHTML, Pattern2);
String YTRedirect = MyMatch2.Groups[1].ToString();
String Temp = YTRedirect.Replace("\\x3d", "=");
String Temp2 = Temp.Replace("\\x26", "&");
YTRedirect = Temp2;

HttpWebRequest PosterRequest = (HttpWebRequest)WebRequest.Create(YTRedirect);
PosterRequest.CookieContainer = cookieContainer;
PosterRequest.Method = "GET";
HttpWebResponse PosterResponse = (HttpWebResponse)PosterRequest.GetResponse();
Stream PosterResponseStream = PosterResponse.GetResponseStream();
PosterResponse.Cookies = PosterRequest.CookieContainer.GetCookies(PosterReq uest.RequestUri);
Encoding PosterEnc = System.Text.Encoding.UTF8;
StreamReader PosterResponseStreamReader = new StreamReader(PosterResponse.GetResponseStream(), PosterEnc, true);
String PosterMyHTML = PosterResponseStreamReader.ReadToEnd();

HttpWebRequest PostestRequest = (HttpWebRequest)WebRequest.Create("http://www.youtube.com/watch?v=Jj41pN8bVtI");
PostestRequest.CookieContainer = cookieContainer;
PostestRequest.Method = "GET";
HttpWebResponse PostestResponse = (HttpWebResponse)PostestRequest.GetResponse();
Stream PostestResponseStream = PostestResponse.GetResponseStream();
PostestResponse.Cookies = PostestRequest.CookieContainer.GetCookies(PostestR equest.RequestUri);
Encoding PostestEnc = System.Text.Encoding.UTF8;
StreamReader PostestResponseStreamReader = new StreamReader(PostestResponse.GetResponseStream(), PostestEnc, true);
String PostestMyHTML = PostestResponseStreamReader.ReadToEnd();

Make sure you replace the text in all capitals with your user name and password since I blocked my own out for this example.
Nov 11 '09 #14
yawar
8
Thanks for the reply buddy, I have tested it and I am getting empty string "YTRedirect" at this code:

String Pattern2 = "location\\.replace\\(\"(http:\\/\\/www\\.youtube\\.com\\/signin[a-zA-Z0-9\\\\\\?\\%_-]*)\"\\)";
Match MyMatch2 = Regex.Match(MyHTML, Pattern2);
String YTRedirect = MyMatch2.Groups[1].ToString();
String Temp = YTRedirect.Replace("\\x3d", "=");
String Temp2 = Temp.Replace("\\x26", "&");
YTRedirect = Temp2;

Here, YTRedirect is returning empty string, I think the pattern you are using has some problem.

Please guide that can we open any of the META refresh URL (META or JAVASCRIPT) or have to open any specific one?

Thanks
Jan 21 '10 #15
Plater
7,872 Expert 4TB
Google API provides this functionality.
Jan 22 '10 #16
@WeCi2i
I'm trying to use this code to automatically login to Google Places (formally known as the Google Local Business Center) from our administration website. The reason for this, is because we have several login accounts that our employees use to manage our accounts. However we don't want to give them all the usernames and passwords. So we want to automatically login them in, so that they can edit the information.

I modified the original code as shown below, and it successfully authenticates and grabs the next page after authentication; however, the httpwebrequest is keeping the authentication and not the browser, so when you try to browse after the login through the browser, it asks for a login.

Does anyone have any idea how I would pass this information back to the browser?

SIDE NOTE TO YAWAR:
There are spaces in the url string that you need to remove to make the original code work, if you look at it, it should be obvious.

Expand|Select|Wrap|Line Numbers
  1.     private void login() {
  2.  
  3.       CookieContainer cookieContainer = new CookieContainer();
  4.       HttpWebRequest PreRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ServiceLogin?utm_source=/places&utm_medium=van&utm_campaign=en&continue=http://www.google.com/local/add/businessCenter%3Fservice%3Dlbc%26gl%3DUS%26hl%3Den-US&service=lbc&hl=en-US&gl=US");
  5.       PreRequest.CookieContainer = cookieContainer;
  6.       PreRequest.AllowAutoRedirect = true;
  7.       HttpWebResponse PreResponse = (HttpWebResponse)PreRequest.GetResponse();
  8.       Stream PreResponseStream = PreResponse.GetResponseStream();
  9.       PreResponse.Cookies = PreRequest.CookieContainer.GetCookies(PreRequest.RequestUri);
  10.       Encoding PreEnc = System.Text.Encoding.UTF8;
  11.       StreamReader PreResponseStreamReader = new StreamReader(PreResponse.GetResponseStream(), PreEnc, true);
  12.       String PreMyHTML = PreResponseStreamReader.ReadToEnd();
  13.  
  14.       String Pattern = "name=\"GALX\"[\\s]*value=\"([a-zA-Z0-9-_\\.]*)\"";
  15.       Match MyMatch = Regex.Match(PreMyHTML, Pattern);
  16.       String GALX = MyMatch.Groups[1].ToString();
  17.  
  18.       HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ServiceLoginAuth?service=lbc&ltmpl=sso&continue=http%3A%2F%2Fwww.google.com%2Flocal%2Fadd%2FbusinessCenter%3Fservice%3Dlbc%26gl%3DUS%26hl%3Den-US&service=lbc&uilel=3&ltmpl=sso&hl=en_US&ltmpl=sso&GALX=" + GALX + "&Email=USERNAME%40gmail.com&Passwd=PASSWORD&rmShown=1&signIn=Sign+in&asts=");
  19.       Request.CookieContainer = cookieContainer;
  20.       Request.Method = "POST";
  21.       Request.Referer = "https://www.google.com/accounts/ServiceLogin?utm_source=/places&utm_medium=van&utm_campaign=en&continue=http://www.google.com/local/add/businessCenter%3Fservice%3Dlbc%26gl%3DUS%26hl%3Den-US&service=lbc&hl=en-US&gl=US";
  22.       Request.MaximumAutomaticRedirections = 50;
  23.       Request.AllowAutoRedirect = true;
  24.       Request.KeepAlive = true;
  25.       Request.ContentLength = 0;
  26.  
  27.       // Get the response
  28.       HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
  29.  
  30.       Stream ResponseStream = Response.GetResponseStream();
  31.       Response.Cookies = Request.CookieContainer.GetCookies(Request.RequestUri);
  32.  
  33.       // Read the response from the stream
  34.       Encoding Enc = System.Text.Encoding.UTF8;
  35.       StreamReader ResponseStreamReader = new StreamReader(Response.GetResponseStream(), Enc, true);
  36.  
  37.       String MyHTML = ResponseStreamReader.ReadToEnd();
  38.       this.Cookies = cookieContainer;
  39.       this.Response.Write(MyHTML);
  40.  
  41.       String Pattern2 = "location\\.replace\\(\"(http:\\/\\/www\\.google\\.com\\/local\\/add\\/businessCenter[a-zA-Z0-9\\\\\\?\\%_-]*)\"\\)";
  42.       Match MyMatch2 = Regex.Match(MyHTML, Pattern2);
  43.       String YTRedirect = MyMatch2.Groups[1].ToString();
  44.       String Temp = YTRedirect.Replace("\\x3d", "=");
  45.       String Temp2 = Temp.Replace("\\x26", "&");
  46.       YTRedirect = Temp2;
  47.  
  48.       HttpWebRequest PosterRequest = (HttpWebRequest)WebRequest.Create(YTRedirect);
  49.       PosterRequest.CookieContainer = cookieContainer;
  50.       PosterRequest.Method = "GET";
  51.       HttpWebResponse PosterResponse = (HttpWebResponse)PosterRequest.GetResponse();
  52.       Stream PosterResponseStream = PosterResponse.GetResponseStream();
  53.       PosterResponse.Cookies = PosterRequest.CookieContainer.GetCookies(PosterRequest.RequestUri);
  54.       Encoding PosterEnc = System.Text.Encoding.UTF8;
  55.       StreamReader PosterResponseStreamReader = new StreamReader(PosterResponse.GetResponseStream(), PosterEnc, true);
  56.       String PosterMyHTML = PosterResponseStreamReader.ReadToEnd();
  57.  
  58.     }
Thanks in Advance,
Ensonix
May 14 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sasquatch | last post by:
I'm having trouble creating a simple login page using the asp:login control. I followed some instructions in a WROX book, "Beginning ASP.NET 2.0," and the instructions are very straight forward,...
3
by: mudgilgaurav | last post by:
I am very keen to learn AJAX and want to use it in my website Can anyone give me a bunch of code abt ajax and as well as tell me the flow of code
1
by: xcelmind | last post by:
Hello Dev. Guru, I want to at this time introduce myself. I am Stanley Ojadovwa by name. I’m a freelance and a newbie in web application development. I’m currently using ASP as my application...
1
by: jaz | last post by:
plzz help me....how to giv login security using sql
4
by: Freedolen | last post by:
Hi All, I had a perl script which is used to login in a web page, but it gives the error as "301 Moved Permanently". What does this means and how can it be rectified? Can anyone help on this? ...
3
by: Ghanathe | last post by:
Hello!!! Can any one tell how to retrieve user name and other details like first name,address,contact no. and so on.... and display in welcome page after login page using c# and sql server...
4
by: John Devlon | last post by:
Hi, I'm trying to create a login page using the default login controle. I also would like to use a custom Ms Acces database instead of a sql server database. Can anyone please tell me how to...
1
yawar
by: yawar | last post by:
Hi, I am a newbie in System.Net programming though I have worked on mshtml but I want to do things more perfectly and with speed. I have created one software that login to gmail using...
33
semanticnotion
by: semanticnotion | last post by:
i want to make a login class using MVC approach. First my html form code is in my view folder then i want authorizing code in controller class and the query and the connection are under my model...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.