13

The newest App Engine SDK (1.2.1) has an API call to compute an ID for a user account based on his email address. (The ID remains the same even if the user changes his address later.) See this question about unique, opaque user IDs for information. However, I have a problem with this API call.

user_id() works for logged-in users (i.e. from users.get_current_user), but it returns None for objects created by the users.User() constructor.** What gives?

For example, using the excellent App Engine Console, this code does not work.

>>> import google.appengine.api.users
>>> me = google.appengine.api.users.get_current_user()
>>> me
users.User(email='jason.h.smith@gmail.com',_user_id='105261964646342707921')
>>> me.user_id()
'105261964646342707921'
>>> somebody = google.appengine.api.users.User('someone@gmail.com')
>>> somebody
users.User(email='someone@gmail.com')
>>>somebody.user_id()
>>> type(somebody.user_id())
<type 'NoneType'>

I want a simple way to convert an email address to a user ID. How can I coerce this ID from Google ahead of time; or if it's not possible, why not?

Edit: Here is the current workaround.

Thanks to Nick Johnson for his answer. Here is his solution in action:

>>> from google.appengine.ext import db
>>> from google.appengine.api import users
>>> class User(db.Model):
...   user = db.UserProperty(required=True)
...
>>> def email_to_userid(address):
...   """Return a stable user_id string based on an email address, or None if
...   the address is not a valid/existing google account."""
...   u = users.User(address)
...   key = User(user=u).put()
...   obj = User.get(key)
...   return obj.user.user_id()
>>> email_to_userid('jason.h.smith@gmail.com')
u'105261964646342707921'
>>> email_to_userid('this@is-an-invalid-email-address')
>>> email_to_userid('this@is-an-invalid-email-address') is None
True
Community
  • 1
  • 1
JasonSmith
  • 72,674
  • 22
  • 123
  • 149

2 Answers2

21

The current workaround is to create a User object, store it to the datastore, and fetch it again. If the email corresponds to a valid Google account, the User object in the returned entity will have its user_id field populated.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • 1
    Thanks. I am testing this right now and if it works, I'll upvote and accept your answer. – JasonSmith May 03 '09 at 11:47
  • +1, It works. I will put some example code in the edited question, so hopefully this question can become the canonical answer for how to convert an email address to a user ID. – JasonSmith May 03 '09 at 12:10
  • Same thing i want to DO For Android. . . Is Anybudy help me Please. . . Thanks in Advance. . . –  May 19 '11 at 09:27
  • Hi Nick, I'm confused, apologies if I'm being dumb. Are you saying with the above workaround that it provides a way of programmatically testing whether the supplied email address is associated with a valid Gmail account - coz that is my legitimate use case. However, first of all I can't get it work using Java. Secondly it seems to contradict this answer, which implies exactly this sort of test is deliberately not supported:http://stackoverflow.com/questions/13313877/how-to-validate-a-gmail-address-in-java/13314114#13314114 – doright Mar 28 '14 at 09:50
  • okay, got it working now, so I guess it is possible. thanks! – doright Mar 28 '14 at 11:23
  • Does anyone have a code snippet for doing this in Java? – Chad Jan 26 '17 at 20:30
1

To make the proposed solution work with NDB, just add use_cache=False, use_memcache=False to the get method. That is: obj = key.get(use_cache=False, use_memcache=False) This guarantees getting the entity from the Datastore with the user_id param populated (only if this is a valid Google account).

ACEGL
  • 149
  • 9