Google 阅读器 API 身份验证

发布于 2024-09-26 18:29:27 字数 1362 浏览 5 评论 0原文

我正在尝试使用以下代码段在 google api 服务上进行身份验证:

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters =
   new ArrayList<HttpMessageConverter<?>> restTemplate.getMessageConverters());
converters.add(new PropertiesHttpMessageConverter());
restTemplate.setMessageConverters(converters);

Properties result = preparePostTo(AUTHENTICATION_URL)
                .using(restTemplate)
                .expecting(Properties.class)
                .withParam("accountType", "HOSTED_OR_GOOGLE")
                .withParam("Email", email)
                .withParam("Passwd", password)
                .withParam("service", "reader")
                .withParam("source", "google-like-filter")
                .execute();
String token = (String) result.get("Auth");

现在我有类似以下的令牌:DQAAAI...kz6Ol8Kb56_afnFc(超过 100 个字符长度)并尝试获取网址:

URL url = new URL(LIKERS_URL + "?i=" + id);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.addRequestProperty("Authorization", "GoogleLogin Auth=" + token);
return url;

但是当我使用此网址获取内容时,我得到 401 客户端错误异常。会是什么?

根据这个问题 Google Reader Authentication Problem 一切都应该没问题。

我只需将网址粘贴到浏览器中即可获取内容。

I am trying to authenticate on google api service using this snippet:

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters =
   new ArrayList<HttpMessageConverter<?>> restTemplate.getMessageConverters());
converters.add(new PropertiesHttpMessageConverter());
restTemplate.setMessageConverters(converters);

Properties result = preparePostTo(AUTHENTICATION_URL)
                .using(restTemplate)
                .expecting(Properties.class)
                .withParam("accountType", "HOSTED_OR_GOOGLE")
                .withParam("Email", email)
                .withParam("Passwd", password)
                .withParam("service", "reader")
                .withParam("source", "google-like-filter")
                .execute();
String token = (String) result.get("Auth");

Now I have token like: DQAAAI...kz6Ol8Kb56_afnFc (more than 100 chars length) and try to get url:

URL url = new URL(LIKERS_URL + "?i=" + id);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.addRequestProperty("Authorization", "GoogleLogin Auth=" + token);
return url;

But while I am getting content using this url I get 401 Client Error exception. What can it be?

According to this question Google Reader Authentication problem all should be fine.

I can get the content simply pasting the url into browser.

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

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

发布评论

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

评论(2

哆啦不做梦 2024-10-03 18:29:27

尝试使用 OAuth 进行 Google Reader 身份验证/授权。您只需使用 OAuth 库并向 Google 注册您的应用程序即可获取 OAuth 使用者密钥/秘密。

您可以使用 oauth.googlecode.com抄写

Try using OAuth for Google Reader authentication/authorization. You just need to use an OAuth library and register your app with Google to get OAuth consumer key/secret.

You can use oauth.googlecode.com or Scribe.

蓝海似她心 2024-10-03 18:29:27

嘿,不知道这是否对您有帮助,或者您是否还关心,但我终于使用我编写的以下 ConsoleApp 代码进入了 Google Reader(请注意,这是 C#,但应该可以轻松转换为 Java)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {

        static void Main(string[] args)
        {
            getAuth();

            Console.ReadLine();
        }

        public static void getAuth()
        {

            //put in the username and password
            string postData = "[email protected]&Passwd=YOURPASSWORD&service=reader&source=some-uniqueapp-v1";

            WebRequest authReq = WebRequest.Create("https://www.google.com/accounts/ClientLogin");
            authReq.ContentType = "application/x-www-form-urlencoded";
            authReq.Method = "POST";

            byte[] bytes = Encoding.ASCII.GetBytes(postData);
            authReq.ContentLength = bytes.Length;
            Stream os = authReq.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);

            WebResponse resp = authReq.GetResponse();

            StreamReader sr = new StreamReader(resp.GetResponseStream());

            string responseContent = sr.ReadToEnd().Trim();

            string[] responseSpilt = responseContent.Split('=');

            string authticket = responseSpilt[3];

            Console.WriteLine("Auth = " + authticket);

            sr.Close();

            getToken(authticket);

        }

        public static void getToken(string auth)
        {

            WebRequest tokenReq = WebRequest.Create("https://www.google.com/reader/api/0/token");
            tokenReq.ContentType = "application/x-www-form-urlendcoded";
            tokenReq.Method = "GET";

            tokenReq.Headers.Add("Authorization", "GoogleLogin auth=" + auth);

            WebResponse response = tokenReq.GetResponse();
            if (response == null) return;

            StreamReader sr = new StreamReader(response.GetResponseStream());
            string respContent = sr.ReadToEnd().Trim();

            string[] respSplit = respContent.Split('/');

            string token = respSplit[2];

            Console.WriteLine(" ");

            Console.WriteLine("Token = " + token);

            sr.Close();

        }
    }
}

Hey, don't know if this will help you or if you even care anymore but I finally got into Google Reader with the following ConsoleApp code I whipped up (note that this is C# but should translate easily to Java).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {

        static void Main(string[] args)
        {
            getAuth();

            Console.ReadLine();
        }

        public static void getAuth()
        {

            //put in the username and password
            string postData = "[email protected]&Passwd=YOURPASSWORD&service=reader&source=some-uniqueapp-v1";

            WebRequest authReq = WebRequest.Create("https://www.google.com/accounts/ClientLogin");
            authReq.ContentType = "application/x-www-form-urlencoded";
            authReq.Method = "POST";

            byte[] bytes = Encoding.ASCII.GetBytes(postData);
            authReq.ContentLength = bytes.Length;
            Stream os = authReq.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);

            WebResponse resp = authReq.GetResponse();

            StreamReader sr = new StreamReader(resp.GetResponseStream());

            string responseContent = sr.ReadToEnd().Trim();

            string[] responseSpilt = responseContent.Split('=');

            string authticket = responseSpilt[3];

            Console.WriteLine("Auth = " + authticket);

            sr.Close();

            getToken(authticket);

        }

        public static void getToken(string auth)
        {

            WebRequest tokenReq = WebRequest.Create("https://www.google.com/reader/api/0/token");
            tokenReq.ContentType = "application/x-www-form-urlendcoded";
            tokenReq.Method = "GET";

            tokenReq.Headers.Add("Authorization", "GoogleLogin auth=" + auth);

            WebResponse response = tokenReq.GetResponse();
            if (response == null) return;

            StreamReader sr = new StreamReader(response.GetResponseStream());
            string respContent = sr.ReadToEnd().Trim();

            string[] respSplit = respContent.Split('/');

            string token = respSplit[2];

            Console.WriteLine(" ");

            Console.WriteLine("Token = " + token);

            sr.Close();

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