我正在尝试编写一个小应用程序来向Google阅读器帐户添加Feed。登录似乎工作正常但是当我尝试添加一个feed时,我收到401 Unauthorized错误。下面是GR对象。我正在使用它。
class GR
{
private string _auth = null;
private string _sid = null;
private string _token = null;
private Cookie _cookie = null;
private string _username;
private string _password;
private void connect()
{
initToken();
}
private void initToken()
{
initSid();
_cookie = new Cookie("SID", _sid, "/", ".google.com");
string url = "https://www.google.com/reader/api/0/token";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "application/x-www-form-urlencoded";
req.Headers.Add("Authorization", "GoogleLogin auth=" + _auth + "");
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader r = new StreamReader(stream);
_token = r.ReadToEnd();
}
}
private void initSid()
{
string requestUrl = string.Format("https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&service=reader&Email={0}&Passwd={1}", _username, _password);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(requestUrl);
req.Method = "GET";
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader r = new StreamReader(stream);
/* TODO : Use propper SubString & IndofOf over Split & Replace */
string cSID = "";
string cLSID = "";
string cAUTH = "";
foreach (string cLine in r.ReadToEnd().Split('\n'))
{
if (cLine.StartsWith("SID=")) { cSID = cLine.Replace("SID=", ""); }
if (cLine.StartsWith("LSID=")) { cLSID = cLine.Replace("LSID=", ""); }
if (cLine.StartsWith("Auth=")) { cAUTH = cLine.Replace("Auth=", ""); }
}
_sid = cSID;
// _lsid = cLSID;
_auth = cAUTH;
}
}
private HttpWebResponse httpGet(string requestUrl, string getArgs)
{
string url = string.Format("{0}?{1}", requestUrl, getArgs);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("Authorization", "GoogleLogin auth=" + _auth + "");
return (HttpWebResponse)request.GetResponse();
}
private HttpWebResponse httpPost(string requestUrl, string postArgs)
{
byte[] buffer = Encoding.GetEncoding(1252).GetBytes(postArgs);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Method = "POST";
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(_cookie);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
Stream PostData = request.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
try
{
return (HttpWebResponse)request.GetResponse();
}
catch
{
//handle error
return null;
}
}
public void Authenticate(string username, string password)
{
_username = username;
_password = password;
connect();
}
public bool AddFeed(string feedUrl)
{
string data = String.Format("quickadd={0}&T={1}", feedUrl, _token);
string url = "http://www.google.com/reader/api/0/subscription/quickadd?client=scroll";
HttpWebResponse response = httpPost(url, data);
if (response == null) return false;
return true;
}
}
使用如下:
private void button1_Click(object sender, EventArgs e)
{
GR gr = new GR();
gr.Authenticate("username", "password");
gr.AddFeed("http://feeds.bbci.co.uk/news/rss.xml");
}
有人能看出我做错了什么吗?代码应该易于运行。只需将您的Google阅读器凭据插入到Authenticate方法调用中即可。