ID 和存取權存證的有效期限有限。如要讓使用者在權杖的有效期限過後繼續登入,請儲存更新權杖,並使用該權杖要求新的權杖。
從 ID 權杖取得使用者個人資料資訊
您可以利用任何 JWT 解碼程式庫將 ID 權杖解碼,以取得登入使用者的個人資訊。例如,使用 Auth0 的 jwt-decode JavaScript 程式庫:
varuser_profile=jwt_decode(<var>id_token</var>);// The "sub" field is available on all ID tokens. This value is unique for each// Google account and can be used to identify the user. (But do not send this// value to your server; instead, send the whole ID token so its authenticity// can be verified.)varuser_id=user_profile["sub"];// These values are available when you request the "profile" and "email" scopes.varuser_email=user_profile["email"];varemail_verified=user_profile["email_verified"];varuser_name=user_profile["name"];varuser_photo_url=user_profile["picture"];varuser_given_name=user_profile["given_name"];varuser_family_name=user_profile["family_name"];varuser_locale=user_profile["locale"];
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eEnable users to sign in to your app using their Google Accounts on devices with limited input capabilities, such as TVs, by displaying a short code and sign-in URL.\u003c/p\u003e\n"],["\u003cp\u003eTo initiate the sign-in process, your app obtains a user code and verification URL from Google's OAuth 2.0 device endpoint, which are then displayed to the user.\u003c/p\u003e\n"],["\u003cp\u003eThe user signs in using a separate device (e.g., a phone or computer) by navigating to the provided URL and entering the code, granting your app access to their Google account.\u003c/p\u003e\n"],["\u003cp\u003eAfter the user grants access, your app can obtain an ID token, access token, and refresh token to identify the user and call Google APIs on their behalf.\u003c/p\u003e\n"],["\u003cp\u003eDecode the ID token to access user profile information, using the \u003ccode\u003esub\u003c/code\u003e field as the unique identifier for the user.\u003c/p\u003e\n"]]],[],null,["# Sign-In on TVs and Limited Input Devices\n\nYou can let users sign in to your app with their Google Accounts on devices with\nlimited input capabilities, such as Internet-connected TVs.\n\nThe app displays a short code and sign-in URL to the user. Then, the user opens\nthe sign-in URL in a web browser, enters the code, and grants the app permission\nto access the user's sign-in information. Finally, the app receives confirmation\nand the user is signed in.\n\nTo use this sign-in flow, the app must run on a device that meets the following\ncriteria:\n\n- The device must be capable of displaying a 40-character URL and a 15-character user code, along with instructions to the user.\n- The device must be connected to the Internet.\n\nGet a client ID and client secret\n---------------------------------\n\nYour app needs an OAuth 2.0 client ID and client secret to make requests to\nGoogle's sign-in endpoints.\n\nTo find your project's client ID and client secret, do the following:\n\n1. Select an existing OAuth 2.0 credential or open the [Credentials page](https://console.cloud.google.com/apis/credentials).\n2. If you haven't done so already, create your project's OAuth 2.0 credentials by clicking **Create credentials \\\u003e OAuth client ID**, and providing the information needed to create the credentials.\n3. Look for the **Client ID** in the **OAuth 2.0 client IDs** section. For details, click the client ID.\n\nIf you are creating a new client ID, select the\n**TVs and Limited Input devices** application type.\n\nObtain a user code and verification URL\n---------------------------------------\n\nOnce a user requests to sign in using a Google Account, you obtain a user code\nand verification URL by sending an HTTP POST request to the OAuth 2.0 device\nendpoint, `https://oauth2.googleapis.com/device/code`. Include your client ID\nand a list of the scopes you need with the request. If you only want to sign in\nusers with their Google Accounts, request only the `profile` and `email` scopes;\nor, if you want to request permission to call a [supported API](/identity/protocols/oauth2/limited-input-device#allowedscopes) on behalf of\nusers, request the required scopes in addition to the `profile` and `email`\nscopes.\n\nThe following is an example request for a user code: \n\n POST /device/code HTTP/1.1\n Host: oauth2.googleapis.com\n Content-Type: application/x-www-form-urlencoded\n\n client_id=\u003cvar translate=\"no\"\u003eYOUR_GOOGLE_CLIENT_ID\u003c/var\u003e&scope=email%20profile\n\nUsing `curl`: \n\n curl -d \"client_id=\u003cvar translate=\"no\"\u003eYOUR_GOOGLE_CLIENT_ID\u003c/var\u003e&scope=email profile\" https://oauth2.googleapis.com/device/code\n\nThe response is returned as a JSON object: \n\n {\n \"device_code\" : \"4/4-GMMhmHCXhWEzkobqIHGG_EnNYYsAkukHspeYUk9E8\",\n \"user_code\" : \"GQVQ-JKEC\",\n \"verification_url\" : \"https://www.google.com/device\",\n \"expires_in\" : 1800,\n \"interval\" : 5\n }\n\nYour app displays the `user_code` and `verification_url` values to the user,\nand, at the same time, polls the sign-in endpoint at the specified `interval`\nuntil either the user signs in or the time specified by `expires_in` has passed.\n\nDisplay the user code and verification URL\n------------------------------------------\n\nAfter you receive a user code and verification URL from the device endpoint,\ndisplay them and instruct the user to open the URL and enter the user code.\n\nThe values of `verification_url` and `user_code` are subject to change. Design\nyour UI in a way that can handle the following limits:\n\n- `user_code` must be displayed in a field wide enough to handle 15 `W`-sized characters.\n- `verification_url` must be displayed in a field wide enough to handle a URL string that is 40 characters long.\n\nBoth strings can contain any printable character from the US-ASCII character\nset.\n\nWhen you display the `user_code` string, don't modify the string in any way\n(such as changing the case or inserting other formatting characters), because\nyour app might break if the format of the code changes in the future.\n\nYou can modify the `verification_url` string by stripping off the scheme from\nthe URL for display purposes if you choose. If you do, be sure your app can\nhandle both \"http\" and \"https\" variants. Don't otherwise modify the\n`verification_url` string.\n\nWhen the user navigates to the verification URL, they see a page similar to the\nfollowing:\n\nAfter the user enters the user code, the Google sign-in site presents a consent\nscreen similar to the following:\n\nIf the user clicks **Allow**, then your app can obtain an ID token to identify\nthe user, an access token to call Google APIs, and a refresh token to acquire\nnew tokens.\n\nObtain an ID token and refresh token\n------------------------------------\n\nAfter your app displays the user code and verification URL, begin polling the\ntoken endpoint (`https://oauth2.googleapis.com/token`) with the device code that\nyou received from the device endpoint. Poll the token endpoint at the interval,\nin seconds, specified by the `interval` value.\n\nThe following is an example request: \n\n POST /token HTTP/1.1\n Host: oauth2.googleapis.com\n Content-Type: application/x-www-form-urlencoded\n\n client_id=\u003cvar translate=\"no\"\u003eYOUR_GOOGLE_CLIENT_ID\u003c/var\u003e&client_secret=\u003cvar translate=\"no\"\u003eYOUR_CLIENT_SECRET\u003c/var\u003e&code=\u003cvar translate=\"no\"\u003eDEVICE_CODE\u003c/var\u003e&grant_type=http://oauth.net/grant_type/device/1.0\n\nUsing `curl`: \n\n curl -d \"client_id=\u003cvar translate=\"no\"\u003eYOUR_CLIENT_ID\u003c/var\u003e&client_secret=\u003cvar translate=\"no\"\u003eYOUR_CLIENT_SECRET\u003c/var\u003e&code=\u003cvar translate=\"no\"\u003eYOUR_DEVICE_CODE\u003c/var\u003e&grant_type=http://oauth.net/grant_type/device/1.0\" https://oauth2.googleapis.com/token\n\nIf the user has not yet approved the request, the response is as follows: \n\n {\n \"error\" : \"authorization_pending\"\n }\n\nYour app should repeat these requests at a rate that does not exceed the value\nof `interval`. If your app polls too quickly, the response is as follows: \n\n {\n \"error\" : \"slow_down\"\n }\n\nOnce the user signs in and grants your app access to the scopes you requested,\nthe response to your app's next request includes an ID token, an access token,\nand a refresh token: \n\n {\n \"access_token\": \"ya29.AHES6ZSuY8f6WFLswSv0HZLP2J4cCvFSj-8GiZM0Pr6cgXU\",\n \"token_type\": \"Bearer\",\n \"expires_in\": 3600,\n \"refresh_token\": \"1/551G1yXUqgkDGnkfFk6ZbjMMMDIMxo3JFc8lY8CAR-Q\",\n \"id_token\": \"eyJhbGciOiJSUzI...\"\n }\n\nUpon receipt of this response, your app can decode the ID token to get basic\nprofile information about the signed-in user, or [send the ID token to your\napp's backend server](/identity/sign-in/web/backend-auth) to securely authenticate with the server. Also, your\napp can use the access token to [call the Google APIs](/identity/protocols/oauth2/limited-input-device#callinganapi) that the user\nauthorized.\n\nID and access tokens have limited lifetimes. To keep the user signed in beyond\nthe tokens' lifetimes, store the refresh token and use it to [request new\ntokens](/identity/protocols/oauth2/limited-input-device#offline).\n\nGet user profile information from the ID token\n----------------------------------------------\n\nYou can get profile information about the signed-in user by decoding the ID\ntoken with any [JWT-decoding library](https://jwt.io/#libraries-io). For example, using the Auth0\n[jwt-decode](https://github.com/auth0/jwt-decode) JavaScript library: \n\n var user_profile = jwt_decode(\u003cvar\u003eid_token\u003c/var\u003e);\n\n // The \"sub\" field is available on all ID tokens. This value is unique for each\n // Google account and can be used to identify the user. (But do not send this\n // value to your server; instead, send the whole ID token so its authenticity\n // can be verified.)\n var user_id = user_profile[\"sub\"];\n\n // These values are available when you request the \"profile\" and \"email\" scopes.\n var user_email = user_profile[\"email\"];\n var email_verified = user_profile[\"email_verified\"];\n var user_name = user_profile[\"name\"];\n var user_photo_url = user_profile[\"picture\"];\n var user_given_name = user_profile[\"given_name\"];\n var user_family_name = user_profile[\"family_name\"];\n var user_locale = user_profile[\"locale\"];\n\n| **Note:** Because you received the ID token directly from Google, you can decode the token without validating it. Whenever you receive an ID token from a source other than Google, such as from a client app to your backend server, you must validate the token to ensure its authenticity. See [Authenticate with a backend\n| server](/identity/sign-in/web/backend-auth#using-a-google-api-client-library).\n| **Key Point:** Only use **`sub`** field as identifier for the user as it is unique among all Google Accounts and never reused. **Don't** use email address as an identifier because a Google Account can have multiple email addresses at different points in time.\n\nMore information\n----------------\n\n- To keep users signed-in beyond the lifetime of an ID token, see [Refreshing\n an access token](/identity/protocols/oauth2/limited-input-device#offline).\n- If you need to authenticate with a backend server, see [Authenticate with a\n backend server](/identity/sign-in/web/backend-auth#using-a-google-api-client-library) for information on doing so securely."]]