1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Youtube Login using C#

Discussion in 'C, C++, C#' started by digall, Mar 25, 2013.

  1. digall

    digall Junior Member

    Joined:
    Feb 3, 2012
    Messages:
    135
    Likes Received:
    13
    I'm trying to login to youtube using following code but no success.Can you figure out what is wrong here.While debugging i find out there is problem with cookies.Entire code is as follows.
    Code:
    public void Login()
            {
                HttpWebRequest request = GetNewRequest("https://accounts.google.com/ServiceLoginAuth", cookies);
                request.Referer = "https://accounts.google.com/ServiceLogin?passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F&uilel=3&hl=en_US&service=youtube";
                Dictionary<string, string> parameters = new Dictionary<string, string>{
                {"continue","https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F"},
                {"service","youtube"},{"uilel","3"},{"dsh","157212168103955870"},{"hl","en_US"},
                {"GALX","PTqcwpZb2aE"},{"pstMsg","1"},{"dnConn",""}, {"checkConnection","youtube%3A248%3A1"}, 
                {"checkedDomains","youtube"}, {"timeStmp",""}, {"secTok",""}, {"Email","username"}, {"Passwd","password"}, 
                {"signIn","Sign+in"}, {"PersistentCookie","yes"}, {"rmShown","1"}};
                HttpWebResponse response = MakeRequest(request, cookies, parameters);
                StreamReader responsereader = new StreamReader(response.GetResponseStream());
                String PreMyHTML = responsereader.ReadToEnd();
                response.Close();
            }
    
    
            private static CookieContainer cookies = new CookieContainer();
    
    
            private static HttpWebRequest GetNewRequest(string targetUrl, CookieContainer SessionCookieContainer)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);
                request.CookieContainer = SessionCookieContainer;
                request.AllowAutoRedirect = false;
                return request;
            }
    
    
            private static HttpWebResponse MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary<string, string> parameters = null)
            {
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Accept: */*";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.CookieContainer = SessionCookieContainer;
                request.AllowAutoRedirect = false;
    
    
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                string postData = string.Empty;
                foreach (KeyValuePair<String, String> parametro in parameters)
                {
                    if (postData.Length == 0)
                        postData += String.Format("{0}={1}", parametro.Key, parametro.Value);
                    else
                        postData += String.Format("&{0}={1}", parametro.Key, parametro.Value);
                }
    
    
                byte[] postBuffer = UTF8Encoding.UTF8.GetBytes(postData);
                using (Stream postStream = request.GetRequestStream())
                {
                    postStream.Write(postBuffer, 0, postBuffer.Length);
                }
    
    
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                SessionCookieContainer.Add(response.Cookies);
    
    
                while (response.StatusCode == HttpStatusCode.Found)
                {
                    response.Close();
                    request = GetNewRequest(response.Headers["Location"], SessionCookieContainer);
                    response = (HttpWebResponse)request.GetResponse();
                    SessionCookieContainer.Add(response.Cookies);
                }
                return response;
            }
    
    I'm lost on code.I'm new to httpwebrequest.Hope you guys will help me.
     
  2. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    617
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    Personally, if you are new to coding / httpwebrequests, start simpler than Youtube. I think you are entering a world of pain.

    But if you want to soldier on, compare the headers as sent from your bot as to those sent by your browser. Go over them line by the line. The reason your bot is failing is in the headers. Always. Without fail. Even if you are sure it isn't, it is. Always.


    If you not sure how to check the headers of your bot. Download fiddler2.

    Good luck
     
  3. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    345
    Likes Received:
    195
    I have a google login function somewhere. i will search for it and will share it here.
     
    • Thanks Thanks x 1
  4. sockpuppet

    sockpuppet Junior Member

    Joined:
    Nov 7, 2011
    Messages:
    155
    Likes Received:
    145
    wild guess: either "dsh" or "GALX" (or both) are anti csrf tokens, you cannot reuse them. you need to load ServiceLoginAuth and extract these values.
     
  5. digall

    digall Junior Member

    Joined:
    Feb 3, 2012
    Messages:
    135
    Likes Received:
    13
    That would be great.I'm trying to make it work but with no success.
     
  6. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    345
    Likes Received:
    195
    This is correct.

    Unfortunately, i am unable to find it. Will recode it from scratch, but it will happen a little later.
     
  7. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    345
    Likes Received:
    195
  8. vebxperts

    vebxperts Jr. VIP Jr. VIP Premium Member

    Joined:
    Nov 15, 2008
    Messages:
    1,657
    Likes Received:
    339
  9. Pa1nstxr

    Pa1nstxr Newbie

    Joined:
    Jun 23, 2013
    Messages:
    19
    Likes Received:
    1
    i can advise you to use fiddler, to see what happens on both sides, youtube and your program. Good luck
     
  10. kingc0der

    kingc0der Newbie

    Joined:
    Jun 1, 2013
    Messages:
    4
    Likes Received:
    2
    Occupation:
    Software Dev/Programmer
    You need to URLEncode the Content you are posting.