如何根据 App Engine 中的电子邮件地址确定 user_id?

发布于 2024-07-18 21:11:01 字数 2423 浏览 11 评论 0原文

最新的 App Engine SDK (1.2.1) 有一个 API 调用,可根据用户帐户的电子邮件地址计算其 ID。 (即使用户稍后更改地址,ID 也保持不变。)请参阅此 有关唯一、不透明的用户 ID 的问题以获取信息。 但是,我对这个 API 调用有疑问。

user_id() 适用于登录用户(即来自 users.get_current_user),但对于 创建的对象则返回 None users.User() 构造函数。** 给出了什么?

例如,使用优秀的 App Engine 控制台,此代码不起作用

>>> import google.appengine.api.users
>>> me = google.appengine.api.users.get_current_user()
>>> me
users.User(email='[email protected]',_user_id='105261964646342707921')
>>> me.user_id()
'105261964646342707921'
>>> somebody = google.appengine.api.users.User('[email protected]')
>>> somebody
users.User(email='[email protected]')
>>>somebody.user_id()
>>> type(somebody.user_id())
<type 'NoneType'>

我想要一种将电子邮件地址转换为用户 ID 的简单方法。 如何提前从 Google 强制获取此 ID; 或者如果不可能,为什么不呢?

编辑:这是当前的解决方法。

感谢尼克·约翰逊的回答。 这是他的实际解决方案:

>>> 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('[email protected]')
u'105261964646342707921'
>>> email_to_userid('this@is-an-invalid-email-address')
>>> email_to_userid('this@is-an-invalid-email-address') is None
True

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='[email protected]',_user_id='105261964646342707921')
>>> me.user_id()
'105261964646342707921'
>>> somebody = google.appengine.api.users.User('[email protected]')
>>> somebody
users.User(email='[email protected]')
>>>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('[email protected]')
u'105261964646342707921'
>>> email_to_userid('this@is-an-invalid-email-address')
>>> email_to_userid('this@is-an-invalid-email-address') is None
True

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城泪 2024-07-25 21:11:01

当前的解决方法是创建一个 User 对象,将其存储到数据存储中,然后再次获取它。 如果电子邮件对应于有效的 Google 帐户,则返回实体中的 User 对象将填充其 user_id 字段。

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.

Smile简单爱 2024-07-25 21:11:01

要使建议的解决方案适用于 NDB,只需将 use_cache=False、use_memcache=False 添加到 get 方法中。 那是:
obj = key.get(use_cache=False, use_memcache=False)
这保证了从数据存储区获取填充了 user_id 参数的实体(仅当这是有效的 Google 帐户时)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文