我可以使用我在网上找到的以下代码使用我的应用程序登录谷歌.它将返回授权码作为响应. Google帮助说,此auth代码应用于发送未来的POST / GET请求.
我需要从中下载Excel格式的电子表格
当我登录google时,我通常可以通过浏览器执行此操作.
如何使用C#中的授权代码发送上述文件的请求?
我在SO中看过一个使用Google Data API的帖子.我不想使用它.
下面是登录的代码示例.它工作正常.
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));
编辑:
根据miffTheFox回复的内容,这就是我得到的回应:
<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>
如果我将流保存为HTML并在浏览器中打开它,则提示下载我需要直接下载的Excel文件.
最佳答案 我实际上做了一个类似于此之前的项目,只是它使用了谷歌阅读器,虽然我假设谷歌授权过程是相同的.
首先,对于您从登录获得的响应中的每个键/值对,您必须将其转换为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"));
}
然后,您必须为您正在进行的请求设置cookie容器.
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();
请享用!
编辑:如何解码返回的HTML!
您需要使用正则表达式来解析URL,然后使用方法对其进行HTML解码.
对我们来说幸运的是,Microsoft在System.Web中提供了一个.只需将对它的引用添加到您的项目中即可.
确保使用System.Text.RegularExpressions添加到文件的顶部!
Match m = Regex.Match("content=\"0; url='(.+)'");
if (!m.Success) throw new Exception(); // Or some other method of making sure the result is okay.
string finalurl = m.Groups[1].ToString();
finalurl = System.Web.HttpUtility.HtmlDecode(finalurl);
然后使用您的CookieContianer获取finalurl!
(这是未经测试的,但应该有用!)