Google Account Information by java code

659 views
Skip to first unread message

Luca Santaniello

unread,
Jun 10, 2011, 6:21:07 AM6/10/11
to google-co...@googlegroups.com
Hi all,

I need invocate google to get information account for my web site. I want insert on my web site new functionality. Registration User page have google button. If user click it, you must insert user and password! I call google api and I get user information and pre-populate his registration form. I don't found simple tutorial.

Can you help me?

Please

Thanks in advance

Alain Vongsouvanh

unread,
Jun 13, 2011, 12:28:44 PM6/13/11
to google-co...@googlegroups.com
Hello Luca,

Please do not ask your users to enter their Google Credentials on your website! If you want a user to grant your website/application authorizations to certain APIs, please use OAuth instead as it adds a layer of security and won't scare away your users who might find unsafe to give their credentials to another website.

Also, you might consider developing your application for Google App Engine as this provides some APIs to retrieve a logged-in user's information: http://code.google.com/appengine/docs/java/users/overview.html

Best,
Alain

--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



--
Alain Vongsouvanh



Luca Santaniello

unread,
Jun 13, 2011, 2:38:45 PM6/13/11
to google-co...@googlegroups.com
Thanks for reply.

I used aouth 2.0 but I have some problem...

1) I invocate "https://accounts.google.com/o/oauth2/auth" passing client_id, redirect_uri, scope (=https://www.google.com/m8/feeds/) and response_type (=code)
2) User redirected to google login page and insert credentials
3) Google validate data and redirect to my page passing "code" parameter
4) I invocate "https://accounts.google.com/o/oauth2/token" passing code, client_id, client_secret, client_secret and grant_type (=authorization_code) but I have this error:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://accounts.google.com/o/oauth2/token

Can you help me please?

Thanks in advance

Alain Vongsouvanh

unread,
Jun 14, 2011, 12:11:15 PM6/14/11
to google-co...@googlegroups.com
Hello Luca,

Seems like you are only missing the last step. Can you copy and paste the content of the body you are POSTing to retrieve the Access Token (in step 4)?
Please make sure to anonymize your client_id and client_secret.

Also, is there a message associated with the 400 status code?

Best,
Alain



Thanks in advance

--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



--
Alain Vongsouvanh



Luca Santaniello

unread,
Jun 14, 2011, 2:20:07 PM6/14/11
to google-co...@googlegroups.com
Thanks for reply...

STEP 1
String clientId = "xxxxxxxxxxx";
String callback = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/myUrl.jsp";  
String authUrl = "https://accounts.google.com/o/oauth2/auth?client_id=" + clientId + "&redirect_uri=" + callback + "&scope=https://www.google.com/m8/feeds/&response_type=code";
response.sendRedirect(authUrl);   

STEP 2 (myUrl.jsp)
String code = request.getParameter("code");
String clientSecret = "yyyyyyyyyyyyyyyyyyyyy";
String newUrl = "https://accounts.google.com/o/oauth2/token";
String clientId = "xxxxxxxxxxxxxxxxxxxxxxxxx";
String callback = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/protectedUrl2.jsp";

String requestUrl = "https://accounts.google.com/o/oauth2/auth?client_id="
            + clientId
            + "&redirect_uri="
            + callback
            + "&scope=https://www.google.com/m8/feeds/&response_type=";

String tokenUrl = new String( "https://accounts.google.com/o/oauth2/token");
   
    StringBuffer params = new StringBuffer("");
    params.append("code=" + code);
    params.append("&client_id=" + clientId);
    params.append("&client_secret=" + clientSecret);
    params.append("&redirect_uri=" + callback);
    params.append("&grant_type=authorization_code");

    try
    {
        // Send data
        URL url = new URL(tokenUrl.toString());
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(params.toString());
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null)
        {
            System.out.println("---" + line);
        }
        wr.close();
        rd.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }


When myUrl.jsp start, I get this error:



java.io.IOException: Server returned HTTP response code: 400 for URL: https://accounts.google.com/o/oauth2/token
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
    at org.apache.jsp.protectedUrl_jsp._jspService(protectedUrl_jsp.java:108)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)


Help me please!

Thanks

Alain Vongsouvanh

unread,
Jun 15, 2011, 12:31:23 PM6/15/11
to google-co...@googlegroups.com
Hey Luca,

It seems that this issue is due to a redirect_url mismatch between the authorization grant and the access token request; the callback URLs should be the same between STEP1 and STEP2. Actually, the callback URL should always be the one from STEP1.

For some reason, the exception doesn't print the error message sent back from the server. One way you could debug is to print or log your request params and use curl to send the request instead of your application so you can get the full error message from the server:


Best,
Alain

--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



--
Alain Vongsouvanh



Luca Santaniello

unread,
Jun 15, 2011, 12:55:46 PM6/15/11
to google-co...@googlegroups.com
Oh thanks!

Rediret Step 1 and Redirect Step 2 is not the same for my example! I try replace it!

I don't know curl. Can you help me?

Thanks

Luca Santaniello

unread,
Jun 15, 2011, 1:04:25 PM6/15/11
to google-co...@googlegroups.com
I solve it! The problem is different redirect Url, sorry!

Now I have my access_token, token_type (=Bearer), expires_in (3600), refresh_token. How can I get current user information as firstname, lastname, email, etc.?

Thanks for all

Alain Vongsouvanh

unread,
Jun 16, 2011, 12:54:19 PM6/16/11
to google-co...@googlegroups.com
Hello Luca,

The Contacts API doesn't let you get the current user's profile information. It let's you get the current user's contacts information.
What you have to do is call the API as described in the various developer's guides:

To authorize your request, specify the OAuth 2 header in your service object:

    myService.setHeader("Authorization", "Oauth <OAUTH_TOKEN>");


Best,
Alain

--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



--
Alain Vongsouvanh



Luca Santaniello

unread,
Jun 16, 2011, 3:12:02 PM6/16/11
to Google Contacts, Shared Contacts and User Profiles APIs
Hi all,

I need invocate google to get information account for my web site. I
invocate OAuth 2.0 but I don't understand very well.

I user 2 method:

Method 1... Step 1 and 2
Step 1
String next = "http://www.example.com/welcome.html";
String scope = "https://www.google.com/m8/feeds/";
boolean secure = false;
boolean session = true;
String authSubLogin = AuthSubUtil.getRequestUrl(next, scope, secure,
session);

It's OK

Step 2
String token = request.getParameter("token");

I get token!!! but then how I can get user information? Where I use my
secret id and password?

Method 2... Step 1 and 2
STEP 1
String clientId = "xxxxxxxxxxx";
String callback = "http://" + request.getServerName() + ":" +
request.getServerPort() + request.getContextPath() + "/myUrl.jsp";
String authUrl = "https://accounts.google.com/o/oauth2/auth?
client_id=" + clientId + "&redirect_uri=" + callback + "&scope=https://
www.google.com/m8/feeds/&response_type=code";
response.sendRedirect(authUrl);

STEP 2 (myUrl.jsp)
String code = request.getParameter("code");
String clientSecret = "yyyyyyyyyyyyyyyyyyyyy";
String newUrl = "https://accounts.google.com/o/oauth2/token";
String clientId = "xxxxxxxxxxxxxxxxxxxxxxxxx";
String callback = "http://" + request.getServerName() + ":" +
request.getServerPort() + request.getContextPath() + "/myUrl.jsp";
and then? How I can get User information?

Which is right version? Method 1 or 2?

Alain Vongsouvanh

unread,
Jun 17, 2011, 7:24:10 PM6/17/11
to google-co...@googlegroups.com
Hello Luca,

Why did you create an other thread for the same topic? I already replied in the other one.

Best,
Alain

--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



--
Alain Vongsouvanh



Luca Santaniello

unread,
Jun 19, 2011, 5:11:23 AM6/19/11
to google-co...@googlegroups.com
Sorry Alan, but I don't understand and I urgent need help. I reed documentation but I don't understand how solve my problem.

I hope you help me. If you can, send me example code, please

Thanks

Alain Vongsouvanh

unread,
Jun 20, 2011, 12:29:14 PM6/20/11
to google-co...@googlegroups.com
Hello Luca,

I still don't understand why you had to create a new thread for the same question. Please use the other thread and reply to the code sample I provided you and tell me what you don't understand.
I won't be able to help you otherwise.

Best,
Alain

--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



--
Alain Vongsouvanh



Luca Santaniello

unread,
Jun 20, 2011, 2:47:48 PM6/20/11
to google-co...@googlegroups.com
Sorry, which thread you said? I don't understand!

If I can, this is my problem:
- mostra testo citato -
and then? How I can get User information?

Which is right version? Method 1 or 2?

Thanks

Alain Vongsouvanh

unread,
Jun 20, 2011, 4:55:52 PM6/20/11
to google-co...@googlegroups.com
Hello Luca,

I already told you that the Contacts API doesn't provide information about the current user but let you retrieve the user's Contacts information.
This has already been answered in the topic you created about your OAuth 2.0 issues.

Best,
Alain

--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



-- 
Alain Vongsouvanh


Luca Santaniello

unread,
Jun 21, 2011, 4:17:00 AM6/21/11
to google-co...@googlegroups.com
Ok, but I don't found this post, sorry!

Luca Santaniello

unread,
Jun 21, 2011, 6:03:32 AM6/21/11
to google-co...@googlegroups.com
Hi Alan, please help me!

Finally, I read all the contacts but I do not know how to read user data logged, as name, last name, email, address...

I used this code, is it right?

STEP 1

String scope = "https://www.google.com/m8/feeds/";
boolean secure = false;
boolean session = true;
String authSubLogin = AuthSubUtil.getRequestUrl(next, scope, secure, session);
response.sendRedirect(authSubLogin);

STEP 2
ContactsService myService = new ContactsService("..........");
String sessionToken = AuthSubUtil.exchangeForSessionToken(token, null);
myService.setAuthSubToken(sessionToken, null);
URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);
System.out.println(resultFeed.getId());

but now I have many answers!

1) Where I set my clientID and my clientSecret?
2) Which feed I call for get user account information? First name, last name, email, etc.?

Thanks

Reply all
Reply to author
Forward
0 new messages