为什么我在发布到 Google Reader API 时收到 401(未经授权)错误?

发布于 2024-08-02 01:23:50 字数 2896 浏览 10 评论 0原文

我正在尝试使用 this 中的信息与 Google Reader(未记录/非官方)API 进行交互页面。 我的第一步是获取 SID 和令牌,这工作正常,但我似乎无法在不收到 401 错误的情况下发布任何内容。

下面是我用来获取 SID 和令牌的代码:

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 并获取令牌:

    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错误。

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

错误发生在 Stream Answer = WebResp.GetResponseStream(); 行上

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋风の叶未落 2024-08-09 01:23:50

您需要仔细检查是否设置了用户代理。 我以前也遇到过同样的问题,当时我没有设置它。

例如:

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

或者: MSDN 链接

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

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;)";
垂暮老矣 2024-08-09 01:23:50

事实证明,由于一些过时的文档,我使用了错误的 URL 来访问 Google Reader API! 在 Google 阅读器中添加标签的正确 URL(截至 2009 年 8 月)是
http://www.google.com/reader/api/ 0/订阅/编辑?client=scroll
带 POST 参数
a=用户/-/标签/[您的标签]&s=feed/[feed url]&ac=edit&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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文