Click here to Skip to main content
14,667,847 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello,
I have been trying for 5 days now with no success, and I really need somebody with more experience to help me.
What I want to do is login to Google Adwords without using their API. I simply need the login part. What I did is traced all httprequests (firebug from firefox) and I managed to do 99% of the code, but something does not work.

What I do is this:
1)Go to google login page: https://www.google.com/accounts/ServiceLogin
2)I successfully login (no problems here) and I am redirected to: https://www.google.com/accounts/ManageAccount
3)Than, I go to adwords by following the link: https://adwords.google.com/um/StartNewLogin?sourceid=awo&subid=ww-en-et-gaia

The problem appears at step 3. I set allowautoredirect=true in the httpwebrequest, and it simply goes in an endless redirect loop. I tried without autoredirect, and it still does not get me to the adwords page.

Here is what I have so far:
public string Login(string username, string password)
        {
            Misc.Download down = new Misc.Download(); // this is my helper class that deals with all requests (post, get)
            string res = down.download("https://www.google.com/accounts/ServiceLogin", "", false); //I first go to point 1 I explained above
            if (res.Contains("gaia captchahtml desc"))
            {
                error = "Captcha is required";
            }
            else
            {
				//I extract 2 variables that I will need to pass in the post parameters
                string galx = System.Text.RegularExpressions.Regex.Match(res, @"(?:name\=""GALX"")(?:.*?)(?:value\="")(.*?)(?:"")",  System.Text.RegularExpressions.RegexOptions.Singleline).Groups[1].Value; 
                string dsh = System.Text.RegularExpressions.Regex.Match(res, @"(?:id\=""dsh"")(?:.*?)(?:value\="")(.*?)(?:"")", System.Text.RegularExpressions.RegexOptions.Singleline).Groups[1].Value;
                username = username.Replace("@", "%40");
                string param = "dsh="+dsh+"&timeStmp=&secTok=&GALX="+galx+"&Email="+username+"&Passwd="+password+"&PersistentCookie=yes&rmShown=1&signIn=Conecta%C5%A3i-v%C4%83&asts=";
                res = down.download("https://www.google.com/accounts/ServiceLoginAuth", param, true);

                //here I go to adwords ... but it does not work
				string redi = System.Text.RegularExpressions.Regex.Match(res, @"(?:\&\#39\;)(.*?)(?:\&\#39\;)", System.Text.RegularExpressions.RegexOptions.Singleline).Groups[1].Value.Replace("&", "&");
				res = down.download(redi, "", false);
				res=down.download("https://adwords.google.com/um/StartNewLogin?sourceid=awo&subid=ww-en-et-gaia", "", false);
            }
            return error;
        }


This is my HttpWebrequest class (That handles post/get). It works good because I use it all the time. I did try to modify everything (headers, cookies ...) but no luck
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.Net.Cache;
using System.Net.Sockets;
using System.Web;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace Misc
{


    public class Headers
    {
        public string header = "";
        public string value = "";
    }
   
    public class Download
    {
        public string error = "";
        public CookieContainer myContainer = new CookieContainer();
        public string referer = "";
        List<Headers> headers = new List<Headers>();
        public List<string> header_name = new List<string>();
        public List<string> header_value = new List<string>();
        public string location = "";
        public bool auto = true;
        public void AddHeader(string header, string value)
        {
            Headers head = new Headers();
            head.header = header;
            head.value = value;
            headers.Add(head);
        }


        public string download(string url, string data, bool post)
        {
            error = "";
            string ln = "";
            //transformam datele in byte array
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data);
            try
            {
                //deschidem conexiunea
                HttpWebRequest r = (HttpWebRequest)WebRequest.Create(url);
                r.CookieContainer = myContainer;


                r.Timeout = 20000;
                r.ReadWriteTimeout = 20000;
                if (referer != "")
                    r.Referer = referer;
                r.Proxy = WebRequest.DefaultWebProxy;
                r.UseDefaultCredentials = true;
                r.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                r.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15";

                //r.Headers.Add("Accept-Encoding", "gzip,deflate");
                r.Headers.Add("Accept-Language", "en-us,en;q=0.5");
                r.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");


                for (int i = 0; i < header_name.Count; i++)
                {
                    r.Headers.Add(header_name[i], header_value[i]);
                }
                if (post)
                {
                    r.Method = "POST";
                    r.ServicePoint.Expect100Continue = false;
                    r.ContentType = "application/x-www-form-urlencoded";
                    if (url.Contains("http://upload.youtube.com/my_videos_upload_json"))
                        r.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                    r.KeepAlive = true;
                }
                else
                {
                    r.ServicePoint.Expect100Continue = true;
                    r.Method = "GET";
                    r.KeepAlive = true;
                    r.ContentType = "text/xml; charset=utf-8";

                }
                r.AllowAutoRedirect = auto;


                if (post)
                {

                    r.ContentLength = buffer.Length;
                    //scriem datele
                    Stream PostData = r.GetRequestStream();
                    PostData.Write(buffer, 0, buffer.Length);
                    PostData.Close();
                }




                //citim ce primim
                HttpWebResponse response = (HttpWebResponse)r.GetResponse();
                
                try
                {
                    this.location = response.Headers["Location"];
                }
                catch (Exception) { }
                /////

                ///////
                Stream webStream = response.GetResponseStream();

                StreamReader sr = new StreamReader(webStream);

                ln = sr.ReadToEnd();

                webStream.Close();
                sr.Close();
                response.Close();
                if (ln == "")
                    ln = "   ";

            }
            catch (Exception ee)
            {
                try
                {
                    using (StreamReader sr = new StreamReader((ee as WebException).Response.GetResponseStream()))
                    {
                        error = sr.ReadToEnd();
                    }
                }
                catch (Exception)
                {
                }
                if (error == "")
                    error = ee.Message;
                return "";
            }
            return ln;

        }
    }
}
</string></string></string></string>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100