aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes [GoogleAPI] get user info Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "[GoogleAPI] get user info" Watch "[GoogleAPI] get user info" New topic
Author

[GoogleAPI] get user info

kevin hack
Greenhorn

Joined: Jun 20, 2011
Posts: 7
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
Roshan Dawrani
Greenhorn

Joined: Aug 27, 2007
Posts: 2
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
Markus Sprunck
Greenhorn

Joined: Apr 11, 2012
Posts: 1
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


Marco Bernardini
Greenhorn

Joined: Aug 28, 2012
Posts: 1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [GoogleAPI] get user info