如果您是使用未對應至有效 Google 帳戶的電子郵件地址來呼叫 User 建構函式,儘管系統還是會建立物件,但該物件無法對應至確切的 Google 帳戶。即使有人在物件儲存後,使用指定電子郵件地址建立 Google 帳戶,也會發生這種情況。建立 User 值時,如果這個值的電子郵件地址不代表「Google 帳戶」,則該值將永遠無法與代表實際使用者的 User 值配對。
在開發網路伺服器下執行應用程式時,系統會假設所有 User 物件在 (模擬) 資料儲存庫中儲存時,這些物件皆代表有效的 Google 帳戶。
有效使用者的 User 物件可為使用者提供唯一識別碼值,即便使用者變更其電子郵件地址,該值仍會維持不變。user_id() 方法會傳回這個 ID,也就是 str 值。
無論應用程式採用何種驗證方式,User 物件的格式均會相同。
將 User 值與資料儲存庫搭配使用
使用者 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"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eThe \u003ccode\u003eUser\u003c/code\u003e class represents a user and provides unique, comparable instances, where equal instances represent the same user.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eusers.get_current_user()\u003c/code\u003e function retrieves the \u003ccode\u003eUser\u003c/code\u003e instance for the currently authenticated user, regardless of the authentication method.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eUser\u003c/code\u003e 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.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003euser_id()\u003c/code\u003e method returns a stable, unique string identifier for a user that persists even if the user changes their email.\u003c/p\u003e\n"],["\u003cp\u003eIt is recommended to store the \u003ccode\u003euser_id\u003c/code\u003e for user identification in the datastore instead of a \u003ccode\u003eUserProperty\u003c/code\u003e because email addresses may change.\u003c/p\u003e\n"]]],[],null,["# User Objects\n\nAn instance of the\n[User](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.users#google.appengine.api.users.User)\nclass represents a user. User instances are unique and comparable. If two instances are equal,\nthen they represent the same user.\n| This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment. If you are updating to the App Engine Python 3 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\nThe application can access the User instance for the current user by calling the\n[users.get_current_user()](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/api/users#get_current_user)\nfunction. \n\n user = users.get_current_user()\n\nYou can use the [users.get_current_user()](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/api/users#get_current_user)\nfunction no matter which authentication option your app uses.\n\nA User instance can be also constructed from an email address: \n\n```python\nuser = users.User(\"Albert.Johnson@example.com\")\n```\n\nOr, if you have a `federated_identity`, you can use it to create a User instance: \n\n```python\nuser = users.User(federated_identity=\"http://example.com/id/ajohnson\")\n```\n\nIf the User constructor is called with an email address that does not correspond with a valid\nGoogle account, the object will be created but it will not correspond with a real Google account.\nThis will be the case even if someone creates a Google account with the given email address after\nthe object is stored. A User value with an email address that does not represent a Google account\nat the time it is created will never match a User value that represents a real user.\n\nWhen running under the development web server, all User objects are assumed to represent valid\nGoogle accounts when stored in the (simulated) datastore.\n\nThe User object for a valid user can provide a unique ID value for the user that stays the same\neven if the user changes her email address. The `user_id()` method returns this ID, a\n`str` value.\n\nThe User object has the same form no matter which method of authentication your app uses.\n\nUsing User Values With the Datastore\n------------------------------------\n\nThe user ID is stable; you can use it in a key name or as a string property.\nTherefore, when using user values, you want to store the user ID (and perhaps the last-seen\nmail address to communicate with the user by mail). The example below shows how to compare the\ncurrent user to a user ID: \n\n class ModelWithUser(ndb.Model):\n user_id = ndb.StringProperty()\n color = ndb.StringProperty()\n\n @classmethod\n def get_by_user(cls, user):\n return cls.query().filter(cls.user_id == user.user_id()).get()\n\nWe strongly recommend that you do not store a `UserProperty`, because it includes the email address\nalong with the user's unique ID. If a user changes their email address and you compare their old, stored\n`User` to the new `User` value, they won't match. Instead, consider using the\n`User` *user ID value* as the user's stable unique identifier."]]