views:

473

answers:

1

I am trying to login to my google account to edit my profile via a Java application using apache httpClient. The code I'm using is given below. Although the code returns a cookie its expiry is set to null and when i check the returned page HTML it is the same as the login page i am trying to connect to.

Can someone please point out the error? The code im using is from ClientFormLogin.java in apache httpclient examples.

public class ClientFormLogin {

public static void main(String[] args) throws Exception {


    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet("https://www.google.com/accounts/Login");

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }
    System.out.println("Initial set of cookies:");
    List<Cookie> cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    HttpPost httpost = new HttpPost("https://www.google.com/accounts/Login"); 

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("Email", "xxxxxx"));
    nvps.add(new BasicNameValuePair("Passwd", "xxxxx"));

    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    response = httpclient.execute(httpost);

    entity = response.getEntity();
    System.out.println("Double check we've got right page " + EntityUtils.toString(entity));

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }

    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    httpclient.getConnectionManager().shutdown();        
}

}

A: 

Hi Hasilk, I am trying the same what u are trying to do..Have you already find solution for that ?? If yes, please let me know. That would be your great help to me..

Thanks in advance..

Rani

Rani
Hi Rani, sadly I couldn't solve this exactly. The problem is that Google doesn't authorize access unless you use their GoogleDataAPI. I managed to update Blogger profiles using the ClientLogin method of GoogleDataAPI and Apache HTTP Client however. You can find the code here http://synchrona-uom.blogspot.com/2010/03/blogger-profile-update.html. I couldn't find the required serviceName for updating Google Profiles though.
hasilk