4

このスニペットを使用して 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 url = new URL(LIKERS_URL + "?i=" + id);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.addRequestProperty("Authorization", "GoogleLogin Auth=" + token);
return url;

しかし、この URL を使用してコンテンツを取得しているときに、401 クライアント エラー例外が発生します。それは何でしょうか?

この質問Google リーダー認証の問題によると、すべて問題ありません。

URLをブラウザに貼り付けるだけでコンテンツを取得できます。

4

2 に答える 2

1

Googleリーダーの認証/承認にOAuthを使用してみてください。OAuthライブラリを使用し、アプリをGoogleに登録するだけで、OAuthコンシューマキー/シークレットを取得できます。

oauth.googlecode.comまたはScribeを使用できます。

于 2011-03-04T12:44:22.020 に答える
0

ねえ、これが役立つかどうか、またはもう気にするかどうかはわかりませんが、最終的に次の ConsoleApp コードを作成して Google リーダーにアクセスしました (これは 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=YOURUSERNAME@gmail.com&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();

        }
    }
}
于 2011-04-08T18:23:51.020 に答える