Difficulties with google authentication

Posted by user283405 on Stack Overflow See other posts from Stack Overflow or by user283405
Published on 2010-03-19T12:37:03Z Indexed on 2010/03/19 13:41 UTC
Read the original article Hit count: 488

I am trying to authenticate google with the following code but google sent me back to the login page again.

//STEP# 1
                string loginURL = "https://www.google.com/accounts/ServiceLoginBox?service=analytics&nui=1&hl=en-US&continue=https%3A%2F%2Fwww.google.com%2Fanalytics%2Fsettings%2F%3Fet%3Dreset%26hl%3Den%26et%3Dreset%26hl%3Den-US";
                request = (HttpWebRequest)WebRequest.Create(loginURL);
                request.CookieContainer = cookieJar;
                request.Method = "GET";             
                request.KeepAlive = true;
                request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/2008111217 Fedora/3.0.4-1.fc10 Firefox/3.0.4";


                HttpWebResponse response = (HttpWebResponse)request.GetResponse();


                foreach (Cookie cook in response.Cookies)
                {
                      cookieJar.Add(cook);
                }


                using (StreamReader sr = new StreamReader(response.GetResponseStream()) )
                {
                    serverResponse = sr.ReadToEnd();                       
                    sr.Close();
                }           

                galx = ExtractValue(serverResponse,"GALX","name=\"GALX\" value=\"");

                Console.WriteLine(galx);


                //Request# 2

                string uriWithData = "https://www.google.com/accounts/ServiceLoginBoxAuth";
                request = (HttpWebRequest)WebRequest.Create(uriWithData);
                request.KeepAlive = true;
                request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/2008111217 Fedora/3.0.4-1.fc10 Firefox/3.0.4";
                request.Method = "POST";
                request.CookieContainer = cookieJar;
                string param = string.Format("Email={0}&Passwd={1}&continue={2}&service=analytics&nui=1&dsh=8209101995200094904&GALX={3}&hl=en-US&PersistentCookie=yes","**my email address**",p,"",galx);
                byte[] postArr = StrToByteArray(param);
                request.ContentType = @"application/x-www-form-urlencoded"; 
                request.ContentLength = param.Length;

                Stream reqStream = request.GetRequestStream();
                reqStream.Write(postArr,0,postArr.Length);
                reqStream.Close();

                response = (HttpWebResponse)request.GetResponse();


                foreach (Cookie cook in response.Cookies)
                {
                      cookieJar.Add(cook);
                }


                using (StreamReader sr = new StreamReader(response.GetResponseStream()) )
                {
                    serverResponse = sr.ReadToEnd();

                    Console.WriteLine(serverResponse);
                    // Close and clean up the StreamReader       
                    sr.Close();
                }           

© Stack Overflow or respective owner

Related posts about c#

Related posts about httpwebrequest