I really did a project that previously did something similar, only it used Google Reader, although I assume that the Google authorization process is the same.
Firstly, for each key / value pair in the response you receive from logging in, you must turn this into a cookie.
string loginResponseText = new StreamReader(loginResponse.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")); }
Then you must set the cookie container for the request you make.
string url = string.Format("http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key={0}&fmcmd=4", documentID); HttpWebRequest rqForFile = (HttpWebRequest)WebRequest.Create(url); rqForFile.CookieContainer = cookies; WebResponse respForFile = rUnread.GetResponse();
Enjoy it!
EDIT : how to decode returned HTML!
You need to use Regex to parse the URL, and then use the method to decode the HTML. Fortunately for us, Microsoft offers one at System.Web. Just add a link to it in your project.
Be sure to use System.Text.RegularExpressions at the top of the file!
Match m = Regex.Match("content=\"0; url='(.+)'"); if (!m.Success) throw new Exception();
Then just run finalurl using your CookieContianer! (This is not tested, but should work!)
source share