Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I determine a user_id based on an email address in App Engine?
    text
    copied!<p>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 <a href="https://stackoverflow.com/questions/778965/generating-unique-and-opaque-user-ids-in-google-app-engine">question about unique, opaque user IDs</a> for information. However, I have a problem with this API call.</p> <p><code>user_id()</code> works for logged-in users (i.e. from <code>users.get_current_user)</code>, but it returns <code>None</code> for objects created by the <code>users.User()</code> constructor.** What gives?</p> <p>For example, using the excellent <a href="http://con.appspot.com" rel="nofollow noreferrer">App Engine Console</a>, this code <strong>does not work</strong>.</p> <pre><code>&gt;&gt;&gt; import google.appengine.api.users &gt;&gt;&gt; me = google.appengine.api.users.get_current_user() &gt;&gt;&gt; me users.User(email='jason.h.smith@gmail.com',_user_id='105261964646342707921') &gt;&gt;&gt; me.user_id() '105261964646342707921' &gt;&gt;&gt; somebody = google.appengine.api.users.User('someone@gmail.com') &gt;&gt;&gt; somebody users.User(email='someone@gmail.com') &gt;&gt;&gt;somebody.user_id() &gt;&gt;&gt; type(somebody.user_id()) &lt;type 'NoneType'&gt; </code></pre> <p>I want a simple way to convert an email address to a user ID. <em>How can I coerce this ID from Google ahead of time; or if it's not possible, why not?</em></p> <h2>Edit: Here is the current workaround.</h2> <p>Thanks to Nick Johnson for his answer. Here is his solution in action:</p> <pre><code>&gt;&gt;&gt; from google.appengine.ext import db &gt;&gt;&gt; from google.appengine.api import users &gt;&gt;&gt; class User(db.Model): ... user = db.UserProperty(required=True) ... &gt;&gt;&gt; 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() &gt;&gt;&gt; email_to_userid('jason.h.smith@gmail.com') u'105261964646342707921' &gt;&gt;&gt; email_to_userid('this@is-an-invalid-email-address') &gt;&gt;&gt; email_to_userid('this@is-an-invalid-email-address') is None True </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload