• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[GoogleAPI] get user info

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

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

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";
- mostra testo citato -
and then? How I can get User information?

Which is right version? Method 1 or 2?

Can you help me?

Please

Thanks in advance
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I assume that you mean you want to fetch the user information for the current user (for whom you have acquired the access token from Google).

If yes, you can get that user's email address by making an authorized call (by passing the access token) from: https://www.googleapis.com/userinfo/email?alt=json (you will find code example if you search for it)

Retrieving other user info, like gender, full name, first/last name, etc is not supported by OAuth 2.0 api. Read here: https://groups.google.com/group/oauth2-dev/browse_thread/thread/a4120fb9cc848abb/c15c27531a4318ae?hl=en&lnk=gst&q=userInfo%23email+fullName

rgds,
Roshan
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With the request "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" + token; where token is a valid token form the user, you get the following information:
- family_name = Doe
- name = John Doe
- locale = en
- gender = male
- email = doe.john@xxx.com
- link = https://plus.google.com/117292523089281814301 (link to Google+ profile)
- given_name = John
- id = 117292523089281814301
- verified_email = True
- picture = (url to Google+ profile picture)

I wrote a tutorial with a minimal in a GWT and Google AppEngine Java application (http://oaae-sample.appspot.com). This sample makes the OAuth2 stuff on the server. This means that you can use it as an API form your application.

Important steps are

A) Preparations to get a Web Applications Starter Project
- Create a web project in Eclipse with the name OAuthAppEngineSample (if you use the same name the next codes changes are easier to do).
- Now, create a new application on Google App Engine. This is necessary to deploy the application.
- Create a new application on Google App Engine Result (success)
- Deploy to AppEngine
- App Engine project settings
- Deploy to AppEngine
- Now you should have a version 1 in your AppEngine (check http://appengine.google.com)

B) Preparations for OAuth2 in Google APIs Console
- Create a client ID in the Google APIs Console (open http://code.google.com/apis/console and click on menu API Access)

C) Modify sample code

The tutorial is to long to post it here (lot screen shots and code), so just go to: http://sites.google.com/site/markussprunck/blog-1/howtogetuserinformationwithoauth2inagwtandgoogleappenginejavaapplication.

D) Further Reading
- Google AppEngine (Introduction); http://developers.google.com/appengine/docs/java/gettingstarted
- Using the Google Plugin for Eclipse: http://developers.google.com/appengine/docs/java/tools/eclipse
- Google Dashboard (Authorized Access to your Google Account); http://www.google.com/dashboard
- Using OAuth 2.0 to Access Google APIs; http://developers.google.com/accounts/docs/OAuth2
- Google AppEngine (Sign up); http://developers.google.com/appengine
- Google APIs; http://code.google.com/apis/console

I hope this helps
Markus


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Markus,
I followed your tutorial and works great!
Apparently the library you're using(com.google.api.gwt.oauth2) doesn't provide the feature to refresh the token.
So, after the token expiration(1h), you need to re-allow the application. This is very annoying...
Did you find any "workaround" on this issue?

Thanks in advance
Marco
reply
    Bookmark Topic Watch Topic
  • New Topic