user2855896
user2855896

Reputation: 270

Android + App engine: user.getUserID() is null in endpoint

I have an Android application with GAE server. I tried to authenticate the user as described on developers.google.com, I added the user parameter to the endpoint methods etc. I get a User which is not null, but this method getUserId() returns null. It is similar to this, rather old problem: Function User.getUserId() in Cloud endpoint api returns null for a user object that is not null But I still don't know how to work around it. How do you handle this error? Have you ever encountered it?

In android client here's what I did (its simplified) :

credentials = GoogleAccountCredential.usingAudience(getApplicationContext(),         "server:client_id:" + WEB_CLIENT_ID);
credentials.setSelectedAccountName(accountName);
WarriorEntityEndpoint.Builder endpointBuilder = new WarriorEntityEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credentials);
warriorEntityEndpoint = endpointBuilder.build();

new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try {
            warriorEntityEndpoint.getWarrior().execute();
        } catch (Exception e) {
        }
        return null;
    }
}.execute();

And on GAE:

@Api(name = "warriorEntityEndpoint", namespace = @ApiNamespace(ownerDomain = "szpyt.com", ownerName = "szpyt.com", packagePath = "mmorpg.monsters"),
version = "version1",
scopes = {"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
clientIds = {Constants.ANDROID_CLIENT_ID, Constants.WEB_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE})
public class WarriorEntityEndpoint {
private static final Logger log = Logger.getLogger(WarriorEntityEndpoint.class.getName());
@ApiMethod(name = "getWarrior")
public WarriorEntity getWarrior(User user) throws OAuthRequestException, IOException  {
    log.log(Level.SEVERE, "this gives correct email: " + user.getEmail());
    log.log(Level.SEVERE, "this is null: " + user.getUserId());

I have also another very important question: is this user authenticated, if getMail() gives me correct account, but getUserId() gives null? I read that user object should be null if it was not authenticated but I am not sure any more... I'm using App engine SDK 1.8.7. I'm testing on a real device and backend deployed to GAE.

Upvotes: 9

Views: 1620

Answers (3)

Creos
Creos

Reputation: 2525

This is a known issue which has been filed with google, I've attached the issue link below.

There are two workarounds (1) save the user and read back from the store, if it refers to a valid account the user id will be populated (this sucks because you pay the saving / loading / deletion cost for each API access that is authenticated even if it is tiny, and obviously some performance cost) and (2) you could use the google+ ID but that is NOT the same as the user id.

This is extremely frustrating and there is currently no ETA as they are working on some fundamental issues with the auth design as far as I understand.

Please, vote for that issue by starring it. You can find all the information here

https://code.google.com/p/googleappengine/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Component%20Status%20Stars%20Summary%20Language%20Priority%20Owner%20Log&groupby=&sort=&id=8848

And here is the current formally approved workaround [(1) above], which you can also find in the link above, but for ease it's here: How can I determine a user_id based on an email address in App Engine?

For workaround (2) mentioned above, you can look at the first link, and go to post #39.

Upvotes: 0

user2855896
user2855896

Reputation: 270

I guess there is no good solution for it right now. I store e-mail as a normal property and remove it from default fetch group, I use long as a primary key (generated by AppEngine) and I query the entity by the e-mail property. I don't like my solution, I'll accept ( and implement :) ) a better one if anyone can provide.

Upvotes: 0

user2072160
user2072160

Reputation: 354

I asked the same question a while ago and got an answer. See link:

Function User.getUserId() in Cloud endpoint api returns null for a user object that is not null

The cause is a bug on appengine.

Upvotes: 2

Related Questions