1

I am trying to get the current email id of the logged in google user. I tried something like the following which works in dev mode but not in production mode.

public class EndpointAPI {

@ApiMethod(httpMethod = HttpMethod.GET, path = "getuser")
public Container getLoggedInUser() {

    UserService userService = UserServiceFactory.getUserService();
    User guser = userService.getCurrentUser();

    Container container = new Container();
    container.user = "user not logged in";

    if (null != guser)
        container.user = guser.getEmail();

    return container;
}

public class Container {
    public String user;
}   
}

I tried looking at the documentation (and tried adding client ids, scope etc) but could not successfully find what I need to do.

If someone can post a simple working example it will be much appreciated.

Regards, Sathya

MikO
  • 18,243
  • 12
  • 77
  • 109
Sathya
  • 1,076
  • 1
  • 8
  • 17

1 Answers1

0

At simplest, you should register a client ID for a web application, and request a User object within the method signature of your API call. Example that supports requests from the JS client:

Ids.java:

public class Ids {
  public static final String WEB_CLIENT_ID = "12345.apps.googleusercontent.com";
}

MyEndpoint.java:

@Api(clientIds = {Ids.WEB_CLIENT_ID})
public class MyEndpoint {
  public getFoo(User user) throws OAuthRequestException {
    if (user != null) {
      // do something with user
    } else {
      throw new OAuthRequestException("Invalid user.");
    }
  }
}

user will automatically be populated with the current user represented by the token passed to your API, or null in the case of an invalid or missing token. The example above throws an exception when there isn't a valid user object, but you can also choose to allow unauthenticated access.

Check out the docs for more.

Dan Holevoet
  • 9,183
  • 1
  • 33
  • 49