4

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.

Community
  • 1
  • 1
Ramesh
  • 41
  • 3
  • Try [readr](https://github.com/shakiba/readr), an unofficial Google Reader client for Java that I have developed. – Ali Shakiba Mar 01 '13 at 17:38

2 Answers2

1

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.

Ali Shakiba
  • 20,549
  • 18
  • 61
  • 88
0

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

        }
    }
}
Jake Sankey
  • 4,977
  • 12
  • 39
  • 53