为什么我得到一个401错误发布到谷歌阅读器API时?阅读器、错误、API、到谷歌

2023-09-05 23:07:16 作者:诗酒趁年少...

我试图与谷歌阅读器(无证/非官方)从的此页面。我的第一个步骤是获得一个SID和令牌,它工作得很好,但我似乎不能没有得到一个401错误张贴任何。

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.

下面是code我用得到我的SID和令牌:

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;
        }
    }

和生成cookie并获得令牌:

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;
        }
    }

现在,如果我尝试做一个POST使用以下(在本例中,插入一个新的标签),我得到的401错误。

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 = "";
    }

上线时,会出现错误流答案= WebResp.GetResponseStream();

推荐答案

原来我用的是错误的URL访问谷歌阅读器的API由于一些过时的文档!正确的网址在谷歌阅读器添加标签(如2009年8月)是 http://www.google.com/reader/api/0 /订阅/编辑?客户端=滚动 与POST参数 A =用户/ - /标签/ [您的标签]放大器; S =进/ [提要URL]放大器; AC =编辑&amp; T公司= [令牌]

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]

 
精彩推荐