Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserService.getCurrentUser() returns null

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 ?


1 Answers

It is required to create send redirect URL :

response.sendRedirect(userService.createLoginURL(request.getRequestURI()));

this will redirect to google login page & then you will be able to fetch Username !

like image 194
user284291 Avatar answered Feb 06 '26 14:02

user284291



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!