I have been unable to export a spreadsheet file using the code below. All I get is the Google Account log in page. I am able to authenticate and store the Auth Id in a cookie container. But my second request for the spreadsheet only leads me to log in page. string str = "/accounts/ClientLogin HTTP/1.0 Content-type: application/x-www-form-urlencoded accountType=GOOGLE&[email protected]&Passwd=....&service=writely&source=Gulp-CalGulp-1.05 ";
string uri = "https://www.google.com/accounts/ClientLogin"; HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri); request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; request.Method = "POST"; byte[] postBytes = Encoding.ASCII.GetBytes(str); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postBytes.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string loginResponseText = new StreamReader (response.GetResponseStream()).ReadToEnd(); CookieContainer cookies = new CookieContainer(); foreach (string ln in loginResponseText.Split('\n')) { if (!ln.Contains("=")) continue; string tId = ln.Substring(0, ln.IndexOf('=')).Trim(); string tVal = ln.Substring(ln.IndexOf('=') + 1).Trim(); cookies.Add(new Cookie(tId, tVal, "/", "www.google.com")); } string sURL = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=0AuT3EANDSDLAdGc0c1RqaS1jVmM5QVdNMjhhRlNnZVE&exportFormat=csv" ; HttpWebRequest wrGETURL; wrGETURL = (HttpWebRequest)WebRequest.Create(sURL); wrGETURL.CookieContainer = cookies; wrGETURL.UseDefaultCredentials = true; Stream objStream; objStream = wrGETURL.GetResponse().GetResponseStream(); string sAllLines = ""; StreamReader objreader = new StreamReader(objStream); sAllLines = objreader.ReadToEnd();
