0

I'm trying to authenticate with Google using a simple Java program. I post to the correct URL with my credentials. I get a response with HTTP status code 200 but that doesn't contain any of the authentication tokens that I need to retrieve feeds for the user. Here's the code

private static String postData = "https://www.google.com/accounts/ClientLogin?Content-type=application/x-www-form-urlencoded&accountType=GOOGLE&Email=xxxxxxxx&Passwd=xxxxx";

public GoogleConnector(){
    HttpClient client=new DefaultHttpClient();
    HttpPost method=new HttpPost(postData);
    try{
        HttpResponse response=client.execute(method);
        System.out.println(response.toString());
    }
    catch(Exception e){ 
    }
2
  • Was that a typo when you were posting your code example or is there really two spaces between Content- and the rest of your URL? Commented Mar 10, 2012 at 13:36
  • ah sorry about that, its just a typo. Commented Mar 10, 2012 at 13:38

1 Answer 1

1

Ok, the first problem you have is that 'Content-Type' needs to be a header, not a request parameter. And secondly, POST parameters should be appended to the request body, not to the request URL. Your code should look something like this:

HttpClient client = new DefaultHttpClient();

HttpPost method = new HttpPost("https://www.google.com/accounts/ClientLogin");
method.setHeader("Content-Type", "application/x-www-form-urlencoded");

List<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>(4);
postParams.add(new BasicNameValuePair("accountType", "GOOGLE"));
postParams.add(new BasicNameValuePair("Email", "xxxxxxx"));
postParams.add(new BasicNameValuePair("Passwd", "xxxxxx"));
postParams.add(new BasicNameValuePair("service", "cl"));

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParams);
method.setEntity(formEntity);

HttpResponse response=client.execute(method);
System.out.println(response.toString());
Sign up to request clarification or add additional context in comments.

6 Comments

It works but still, I don't get the SID, LSID and Auth cookies in the response. I do get a status 200 which means that it should be authenticated but I don't get the tokens
When I POST this request in REST Client I get the SID and LSID in the response body, not headers.
Great it works, thank you. But from what I read, SID and LSID should not be used and the Auth token should be used instead. Thing is, I can't find the Auth token in the response body. Any thoughts on that?
Looks like you also need to specify the service parameter in order to get back an Auth token.
Yes that's right, perfect :)) Thank you very much. Just a quick note, for others who come and read this post the parameter is "Passwd" and not "Password" so you might wanna edit that just to avoid confusion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.