如果使用与有效 Google 账号不对应的电子邮件地址调用 User 构造函数,则会创建对象,但该对象与实际的 Google 账号不对应。
即使有人在存储了该对象后用指定的电子邮件地址创建了 Google 账号,也将出现这种情况。如果创建 User 值时使用的电子邮件地址当时不代表 Google 账号,该 User 值将永远不会与代表真实用户的 User 值相匹配。
当在开发 Web 服务器下运行时,所有 User 对象在存储于(模拟)Datastore 中时都假定代表有效的 Google 账号。
有效用户的 User 对象可以为该用户提供唯一 ID 值,即使该用户更改了其电子邮件地址,该 ID 值也保持不变。user_id() 方法会返回此 ID,这是一个 str 值。
无论您的应用使用哪种身份验证方法,User 对象均采用相同的形式。
将 User 值与 Datastore 配合使用
用户 ID 是稳定的;您可以在键名或字符串属性中使用。
因此,当使用用户值时,您需要存储用户 ID(或许是最后一次看到的邮件地址,以便通过邮件与用户通信)。以下示例展示了如何将当前用户与用户 ID 进行比较:
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-06。"],[[["The `User` class represents a user and provides unique, comparable instances, where equal instances represent the same user."],["The `users.get_current_user()` function retrieves the `User` instance for the currently authenticated user, regardless of the authentication method."],["`User` instances can be created using an email address or a federated identity, but those created from invalid Google accounts will not match real user accounts."],["The `user_id()` method returns a stable, unique string identifier for a user that persists even if the user changes their email."],["It is recommended to store the `user_id` for user identification in the datastore instead of a `UserProperty` because email addresses may change."]]],[]]