如何根据 App Engine 中的电子邮件地址确定 user_id? [英] How can I determine a user_id based on an email address in App Engine?

查看:26
本文介绍了如何根据 App Engine 中的电子邮件地址确定 user_id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

user_id() 适用于登录用户(即来自 users.get_current_user),但对于由users.User() 构造函数.** 给出了什么?

例如,使用优秀的App Engine Console,此代码不起作用.

<预><代码>>>>导入 google.appengine.api.users>>>我 = google.appengine.api.users.get_current_user()>>>我users.User(email='jason.h.smith@gmail.com',_user_id='105261964646342707921')>>>me.user_id()'105261964646342707921'>>>有人 = google.appengine.api.users.User('someone@gmail.com')>>>某人users.User(email='someone@gmail.com')>>>somebody.user_id()>>>类型(somebody.user_id())<输入'NoneType'>

我想要一种将电子邮件地址转换为用户 ID 的简单方法.我怎样才能提前从谷歌强制这个ID;或者如果不可能,为什么不呢?

这是当前的解决方法.

感谢尼克约翰逊的回答.这是他的解决方案:

<预><代码>>>>从 google.appengine.ext 导入数据库>>>从 google.appengine.api 导入用户>>>类用户(db.Model):... 用户 = db.UserProperty(required=True)...>>>def email_to_userid(地址):... """根据电子邮件地址返回一个稳定的 user_id 字符串,或者 None 如果...该地址不是有效/现有的 Google 帐户."""... u = users.User(地址)... key = User(user=u).put()... obj = User.get(key)...返回 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') 是 None真的

解决方案

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

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

解决方案

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.

这篇关于如何根据 App Engine 中的电子邮件地址确定 user_id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆