[[["容易理解","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"]],["上次更新時間:2024-11-09 (世界標準時間)。"],[[["\u003cp\u003eGoogle Identity Services provides a JavaScript library to simplify OAuth 2.0 flows for web applications, enabling access to Google APIs.\u003c/p\u003e\n"],["\u003cp\u003eFor authentication using \u003ccode\u003eemail\u003c/code\u003e, \u003ccode\u003eprofile\u003c/code\u003e, or \u003ccode\u003eopenid\u003c/code\u003e scopes, consider using a JWT ID Token and Sign In With Google for a simpler approach.\u003c/p\u003e\n"],["\u003cp\u003eThe authorization code flow is generally recommended for better security and includes a refresh token for ongoing access.\u003c/p\u003e\n"],["\u003cp\u003eUsers must grant consent for your app to access requested data scopes through a Google-displayed dialog.\u003c/p\u003e\n"],["\u003cp\u003eYou can revoke tokens and consent using the \u003ccode\u003egoogle.accounts.oauth2.revoke\u003c/code\u003e method or through the user's Google Account settings.\u003c/p\u003e\n"]]],[],null,["# How user authorization works\n\nIf you are new or unfamiliar with Google Identity Services or authorization,\nstart by reading the [Overview](/identity/oauth2/web/guides/overview).\n\nGoogle offers a JavaScript library which includes authorization features to help\nyou to manage scopes, obtain user consent, and more easily work with standard\nOAuth 2.0 flows. Your web application, running in the user's browser, uses this\nlibrary to manage the OAuth 2.0 implicit flow, or to start the authorization\ncode flow which finishes on your backend platform.\n\n### Authentication only scopes\n\nSeveral scopes are used only for user authentication: `email`, `profile`, and\n`openid`. If your app only uses these scopes, consider if a JWT ID Token and\n[Sign In With Google](/identity/gsi/web) for user sign-up and sign-in meets your needs. In most\ncases, this is the most straightforward and simple method available for user\nauthentication.\n\nKey terms and concepts\n----------------------\n\nThese guides assume that you have a basic understanding of OAuth 2.0 concepts\nand IETF standards such as [RFC6749](https://datatracker.ietf.org/doc/html/rfc6749). The following terms\nare used throughout authorization guides:\n\n- **Access token** is a short-lived per user credential issued by Google that is used to securely call Google APIs and access user data.\n- **Authorization code** is a temporary code issued by Google to securely identify individual users who sign-in to their Google Account from a browser. Your backend platform exchanges this code for access and refresh tokens.\n- **Refresh token** is a long-lived per user credential issued by Google that is securely stored on your platform and can be used to obtain a new, valid access token even when the user is not present.\n- **Scope** restricts tokens to a defined and limited amount of user data, see [OAuth 2.0 Scopes for Google APIs](/identity/protocols/oauth2/scopes) for more.\n- **Popup mode** is an authorization code flow based upon a JavaScript callback running in the user's browser. Google invokes your callback handler which is then responsible for sending the auth code to your platform, how this is done is up to you.\n- **Redirect mode** is an authorization code flow based upon HTTP redirects. The user-agent is first redirected to Google, a second redirect from Google to your platform's authorization code endpoint includes the code.\n\nToken lifetimes are set by Google, as the issuer. Due to various factors the\nexact duration may vary.\n\nOAuth 2.0 flows\n---------------\n\nTwo flows, implicit and authorization code are discussed. Both return an access\ntoken suitable for use with Google APIs.\n\nThe authorization code flow is recommended as it offers improved user security.\nThis flow also returns a refresh token which can be used to obtain access tokens\nwithout the user being present, enabling your platform to more easily perform\nasynchronous actions such as sending an SMS reminder of an upcoming meeting that\nwas scheduled at the last minute. [Choose an authorization model](/identity/oauth2/web/guides/choose-authorization-model) explains\nthe differences between the two flows in more detail.\n\nThe Google Identity Services JavaScript library follows the OAuth 2.0 standard\nto:\n\n- manage the [implicit](https://datatracker.ietf.org/doc/html/rfc6749#section-1.3.2) flow to enable your in-browser web app to quickly and easily obtain an access token from Google that is necessary to call Google APIs.\n- start the [authorization code](https://datatracker.ietf.org/doc/html/rfc6749#section-1.3.1) flow from the user's browser.\n\n### Common steps\n\nBoth the implicit and authorization code flow begin the same way:\n\n1. Your app requests access to one or more scopes.\n2. Google displays a consent dialog to the user, and if necessary first signs the user into their Google Account.\n3. The user individually approves each requested scope.\n\nEach flow then finishes with different steps.\n\n### When using the implicit flow\n\n- Google uses a callback handler to notify your app of the consent outcome and return an access token for any approved scopes.\n\n### When using the auth code flow\n\n- Google responds with a per user authorization code:\n - In redirect mode, the code is returned to your platform's authorization code endpoint.\n - In popup mode, the code is returned to your in-browser app's callback handler, without users needing to leave your website.\n- Beginning at [Step 4: Handle the OAuth 2.0 server response](/identity/protocols/oauth2/web-server#handlingresponse) your backend platform completes a server-to-server exchange with Google, ultimately resulting in a per user refresh token and access token being returned to your platform.\n\nUser consent\n------------\n\nPrior to obtaining an access token individual users must grant consent for your\napp to access the requested scopes. To do so, Google displays a consent dialog\nduring Step 2 above and records the result in\n[myaccount.google.com/permissions](https://myaccount.google.com/permissions).\n\nYour app name, logo, privacy policy, terms of service and requested scopes are\ndisplayed to the user along with the option to approve or cancel the request.\n\nIn figure 1, the consent dialog for a single scope is shown. When a single scope\nis requested, no checkboxes are necessary to approve or deny a scope.\n\n**Figure 1:** User consent dialog with a single scope.\n\nIn figure 2, the consent dialog for multiple scopes is shown. When more than one\nscope is requested individual checkboxes are necessary to allow the user to\napprove or to deny each scope.\n\n**Figure 2:** User consent dialog with multiple scopes.\n\n### User accounts\n\nA Google Account is required to record consent and to issue an access token.\nPrior to this, individual users must have authenticated themselves to Google by\nsigning-in to a Google Account.\n\nWhile not a requirement, it is recommended that Sign In With Google be used for\nsign-up and sign-in to your web app or backend platform. Doing so reduces user\nfriction by minimizing the number of required steps and optionally enables you\nto easily associate access tokens with individual accounts on your platform.\n\nFor example, using Sign In With Google establishes an active Google Account\nsession, thus avoiding the need to later prompt the user to sign-in to a Google\nAccount when making an authorization request. If you choose to authenticate\nusers to your app by other means such as username and password, or other\nidentity providers, they will still be required to first sign in to a Google\nAccount for consent.\n\nAdding a [login hint](/identity/oauth2/web/reference/js-reference#TokenClientConfig) during authorization initialization--typically the\nemail address of the user's Google Account--enables Google to skip the display\nof an account chooser, saving users a step. The ID Token credential returned by\nSign In With Google contains the user's email address.\n\nWeb apps running only in the browser may rely solely on Google for user\nauthentication, choosing to not to implement a user account management system.\nIn this scenario, known as the implicit flow, there is no need to associate a\nrefresh token with a user account and management secure storage.\n\nAlternatively, a user account system is required by the authorization code flow.\nPer user refresh tokens must be associated with an individual account on your\nbackend platform and stored for later use. How to implement, work with, and\nmanage a user account system is unique to your platform and is not discussed in\nmore detail.\n\n### Viewing and revoking consent\n\nUsers may view or revoke consent at any time from their Google Account settings.\n\nOptionally, your web app or platform can call\n[`google.accounts.oauth2.revoke`](/identity/oauth2/web/guides/use-token-model#using_an_access_token_to_revoke_consent) to revoke tokens and remove user consent,\nuseful when a user deletes their account from your platform.\n\nOther authorization options\n---------------------------\n\nAlternatively, browsers may obtain access tokens using the implicit flow by\ndirectly calling Google's OAuth 2.0 Endpoints as described by [OAuth 2.0 for\nClient-side Web Applications](/identity/protocols/oauth2/javascript-implicit-flow).\n\nSimilarly, for the authorization code flow you may choose to implement your own\nmethods and follow the steps outlined in [Using OAuth 2.0 for Web Server\nApplications](/identity/protocols/oauth2/web-server).\n\nIn both cases we strongly recommend using the Google Identity Services library\nto reduce your development time and effort and to minimize security risks such\nas those described by [OAuth 2.0 Security Best Current\nPractice](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-security-topics)."]]