I'm able to login to google with my application with the following code which I found from web. It is returning an Authorization Code as response. Google help says that this auth code should be used to send future POST/GET requests.
I need to download a spreadsheet in Excel format from http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=DOCUMENT_ID&fmcmd=4 which I can normally do from my browser when I'm logged in to google.
How can I send a request for the above file with the Authorization code in C#? I have seen a thread in SO which used Google Data API. I don't want to make use of it.
Below is the code sample for logging in. It works fine.
string str = "/accounts/ClientLogin HTTP/1.0 Content-type: application/x-www-form-urlencoded accountType=GOOGLE&Email=myname@gmail.com&Passwd=password&service=cl&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();
StringBuilder sb = new StringBuilder();
string webresponse = new StreamReader(response.GetResponseStream()).ReadToEnd();
int AuthIndex = webresponse.IndexOf("Auth=");
sb.Append(webresponse);
sb.Append("\n");
sb.Append(response.StatusCode);
richTextBox1.Text = sb.ToString();
string authCode = webresponse.Substring(AuthIndex + 5, webresponse.Length - (AuthIndex + 5));
EDITED: This is what I get as response when I did according to what miffTheFox has replied:
<html><head><title>Redirecting</title>
<meta http-equiv="refresh" content="0; url='http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=p_zC6U3bOsLTuXeUdmQI1RA&fmcmd=4&pli=1&auth=DQAAAIoAAAAfbQUnX8EaZzQcBSIRJSeU4xtFF6ITt9069JLJyJsoqFGMzSE8HrvArHmGPoA-Wf2CbhnDQv_bGKXye2_qyL6EAhTEdOs6Alz-VMeYFsqdGlYjxospBokgCO1958kSVuVFRe9UuKkfV2f_6ZX8SROMkMNdMz3MW8Wh3UNmflIX4E92CpnMleSjCRVpH9x5gSQ&gausr=username%40gmail.com'"></head>
<body bgcolor="#ffffff" text="#000000" link="#0000cc" vlink="#551a8b" alink="#ff0000"><script type="text/javascript" language="javascript">
location.replace("http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key\x3dp_zC6U3bOsLTuXeUdmQI1RA\x26fmcmd\x3d4\x26pli\x3d1\x26auth\x3dDQAAAIoAAAAfbQUnX8EaZzQcBSIRJSeU4xtFF6ITt9069JLJyJsoqFGMzSE8HrvArHmGPoA-Wf2CbhnDQv_bGKXye2_qyL6EAhTEdOs6Alz-VMeYFsqdGlYjxospBokgCO1958kSVuVFRe9UuKkfV2f_6ZX8SROMkMNdMz3MW8Wh3UNmflIX4E92CpnMleSjCRVpH9x5gSQ\x26gausr\x3dusername%40gmail.com")
</script></body></html>
If I save the stream as HTML and open it in browser it is prompting for downloading the Excel file which I need to download directly.