Why am I getting a 401 (Unauthorized) error when POSTing to Google Reader API?

♀尐吖头ヾ 提交于 2019-12-08 07:04:00

问题


I'm trying to interface with the Google Reader (undocumented/unofficial) API using information from this page. My first step is to get a SID and token, which works fine, but I can't seem to POST anything without getting a 401 error.

Here is the code I'm using to get my SID and token:

static string getSid()
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin?service=reader&Email=username&Passwd=password");
        req.Method = "GET";
        string sid;

        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        using (var stream = response.GetResponseStream())
        {
            StreamReader r = new StreamReader(stream);
            string resp = r.ReadToEnd();

            int indexSid = resp.IndexOf("SID=") + 4;
            int indexLsid = resp.IndexOf("LSID=");
            sid = resp.Substring(indexSid, indexLsid - 5);

            return sid;
        }
    }

And to generate a cookie and get the token:

    static Cookie getCookie(string sid)
    {
        Cookie cookie = new Cookie("SID", sid, "/", ".google.com");
        return cookie;
    }

    static string getToken(string sid, Cookie cookie)
    {
        string token;
        string url = "http://www.google.com/reader/api/0/token";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.CookieContainer = new CookieContainer();
        req.CookieContainer.Add(cookie);

        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        using (var stream = response.GetResponseStream())
        {
            StreamReader r = new StreamReader(stream);
            token = r.ReadToEnd();
            return token;
        }
    }

Now if I try to do a POST (in this example, insert a new tag) using the following, I get the 401 error.

    static void insertTag(string tag, Cookie cookie)
    {
        string result = "";
        string data = Uri.EscapeDataString("a="+tag+"&T=" + Program.token);

        byte[] buffer = Encoding.GetEncoding(1252).GetBytes(data);
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create
            ("http://www.google.com/reader/api/0/edit-tag?client=-");
        WebReq.Method = "POST";

        WebReq.Credentials = new NetworkCredential("username", "password");


        WebReq.ContentType = "application/x-www-form-urlencoded";
        WebReq.ContentLength = buffer.Length;

        Stream PostData = WebReq.GetRequestStream();

        PostData.Write(buffer, 0, buffer.Length);
        PostData.Close();

        HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
        Stream Answer = WebResp.GetResponseStream();
        StreamReader _Answer = new StreamReader(Answer);
        result = _Answer.ReadToEnd();

        if (result.Length < 0)
            result = "";
    }

The error occurs on the line Stream Answer = WebResp.GetResponseStream();


回答1:


You need to double check that you have a user agent set. I have run into this same problem before when i didnt have it set.

For example:

WebClient client = new WebClient();

client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Or: MSDN Link

myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
myHttpWebRequest.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";



回答2:


Turns out I was using the wrong URL to access the Google Reader APIs thanks to some outdated documentation! The correct URL for adding labels in Google Reader (as of August 2009) is http://www.google.com/reader/api/0/subscription/edit?client=scroll with POST arguments a=user/-/label/[your label]&s=feed/[feed url]&ac=edit&T=[token]



来源:https://stackoverflow.com/questions/1275493/why-am-i-getting-a-401-unauthorized-error-when-posting-to-google-reader-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!