Why am I getting a 401 (Unauthorized) error when P

2019-06-04 20:24发布

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();

2条回答
叛逆
2楼-- · 2019-06-04 21:06

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]

查看更多
乱世女痞
3楼-- · 2019-06-04 21:14

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;)";
查看更多
登录 后发表回答