I have implemented 3-legged OAuth using Google OAuth API in Java.
My application is registered on GAE. And, I am successfully getting the access token as well. But, now when I am creating the object of UserService, even if I am logged in Gmail as well as my personal domain mail, it returns null while calling getCurrentUser() !
My code is :
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
oauthParameters.setScope("https://spreadsheets.google.com/feeds/");
oauthParameters.setOAuthType(OAuthParameters.OAuthType.THREE_LEGGED_OAUTH);
oauthParameters.setOAuthToken(request.getSession().getAttribute("oauth_token").toString());
oauthParameters.setOAuthTokenSecret(request.getSession().getAttribute("oauth_token_secret").toString());
UserService userService = UserServiceFactory.getUserService();
out.println("<br/>Get Current User : " + userService.getCurrentUser());
There is no need to specifically add any jar while using User Service. Then what is the problem here ?
Also when I try this :
out.println(userService.isUserLoggedIn() ? "User is Logged In" : "User is Log Off");
It outputs :
User is Log Off
while I am login to my personal domain & gmail both ! I think, this should probably redirect me to login page in case if I am sign off. But, it doesn't !
Also, as per it given here & in systempuntout Answer, if I try :
OAuthService oauth = OAuthServiceFactory.getOAuthService();
User user = oauth.getCurrentUser();
out.println("<br/>Get Current User : " + user.getNickname());
Still, it stops the execution ! :(
I am getting following error in Google App Engine Log :
Oauth.requestServlet doGet: null
com.google.appengine.api.oauth.InvalidOAuthParametersException: Unknown
at com.google.appengine.api.oauth.OAuthServiceImpl.makeSyncCall(OAuthServiceImpl.java:73)
at com.google.appengine.api.oauth.OAuthServiceImpl.getGetOAuthUserResponse(OAuthServiceImpl.java:53)
at com.google.appengine.api.oauth.OAuthServiceImpl.getCurrentUser(OAuthServiceImpl.java:28)
at Oauth.accessFeeds.access(accessFeeds.java:60)
at Oauth.requestServlet.processRequest(requestServlet.java:129)
at Oauth.requestServlet.doGet(requestServlet.java:199)
For reference : Using the Users Service For APIs : com.google.appengine.api.oauth, com.google.appengine.api.users
Is it (least possible) bug with Google OAuth ?
I had a similar problem and it happened due to a signature problem.
My app was sending the parameters inside the request body, performing a POST with "application/x-www-form-urlencoded" mime type.
When OAuthParameters signs the request, it includes all the query parameters in the data to be signed. In this case, the parameters were empty.
Moving the request parameters from HTTP request body to URL query string solved the problem.
Edited:
It seems like it fails when there is more than one parameter with the same name.
Both, parameters in HTTP body (using "application/x-www-form-urlencoded") and duplicated parameter names should be taken into account when building the signature according to 9.1.1. Normalize Request Parameters.
But it looks like OAuthParameters doesn't consider these two possibilities on method "computeSignature()" (line 137). In fact, at line 157 we have the following code:
which keeps only the last value for a repeated parameter?
Edited 2:
The encoded parameter problem is reported in the issue number 1: https:// code.google.com/p/google-oauth-java-client/issues/detail?id=1. It is a known issue. At javadocs: http:// javadoc.google-api-java-client.googlecode.com/hg/1.2.2-alpha/com/google/api/client/auth/oauth/package-summary.html it is included into non supported features.
Related post: Using Google API Java Client on Android, a POST request does not seem to authenticate with Google App Engine app using OAuth
It is required to create send redirect URL :
this will redirect to google login page & then you will be able to fetch Username !