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