Piqué
Piqué

Reputation: 11

AppEngine Users Service's User Constructor with Valid Email Returns User without user_id

My understanding when reading AppEngine Users Service documentation https://developers.google.com/appengine/docs/python/users/userobjects is that if we initialize a user with a valid email address, the created user should have a valid user_id. However, when I tried this code

from google.appengine.api import users

user = users.User('[email protected]')
print user.user_id()

on both development server and real AppEngine environment, I always get

None

Is this a bug? Or do I misunderstand the document?

Upvotes: 1

Views: 141

Answers (1)

RocketDonkey
RocketDonkey

Reputation: 37269

If the object is instantiated as you do above, it will actually return None. Per the docs:

Note: If your application constructs the User instance, the API will not set a value for user_id and it returns None

In order to use the user_id method, I would suggest using the user = users.get_current_user() structure, and if the user is not logged in, allowing them to authenticate with their Google account. For example:

user = users.get_current_user()
if user:
    self.response.out.write(user.user_id())
else:
    self.response.out.write(
        "<a href=\"%s\">Sign in</a>." % users.create_login_url("/"))

You can find similar examples in the documentation, but after authentication, you will be able to access the user_id method.

Upvotes: 2

Related Questions