最佳做法
透過集合功能整理內容 你可以依據偏好儲存及分類內容。
本頁將說明整合 OAuth 2.0 的一般最佳做法。除了應用程式類型和開發平台的特定指南外,請考慮採用這些最佳做法。另請參閱應用程式正式版發布相關建議和 Google 的 OAuth 2.0 政策。
安全處理使用者權杖
使用者權杖包含應用程式使用的更新權杖和存取權杖。請安全地儲存權杖,並絕不以明文傳輸。使用適合您平台的安全儲存系統,例如 Android 上的 Keystore、iOS 和 macOS 上的 Keychain 服務,或 Windows 上的憑證保管箱。
撤銷不再需要的權杖,並從系統中永久刪除。
此外,請考慮採用下列平台最佳做法:
處理更新權杖的撤銷和到期
如果應用程式已要求離線存取權的重新整理權杖,您也必須處理權杖失效或到期的問題。權杖可能會因不同原因而失效,例如可能已過期,或應用程式的存取權已遭使用者或自動化程序撤銷。在這種情況下,請仔細考量應用程式應如何回應,包括在使用者下次登入時提示他們或清理資料。如要接收權杖撤銷通知,請整合跨帳戶防護服務。
使用增量授權
當應用程式需要功能時,請使用漸進式授權要求適當的 OAuth 範圍。
除非應用程式的核心功能需要存取資料,否則您不應在使用者首次驗證時要求存取資料。相反地,您應遵循「選取最小、最受限的範圍」原則,只要求執行工作所需的特定範圍。
請務必在相關情境中要求範圍,協助使用者瞭解應用程式要求存取權限的原因,以及資料的使用方式。
舉例來說,您的應用程式可能會遵循以下模式:
- 使用者透過應用程式進行驗證
- 不會要求其他範圍。應用程式提供基本功能,讓使用者探索及使用不需要任何額外資料或存取權的功能。
- 使用者選取需要存取額外資料的功能
- 應用程式會針對這項功能所需的特定 OAuth 範圍,提出授權要求。如果這項功能需要多個範圍,請遵循下列最佳做法。
- 如果使用者拒絕要求,應用程式會停用該功能,並提供使用者其他情境,以便再次要求存取權。
處理多個範圍的同意聲明
一次要求多個範圍時,使用者可能不會授予您要求的所有 OAuth 範圍。應用程式應透過停用相關功能,處理權限遭拒的情況。
如果應用程式的基本功能需要多個範圍,請先向使用者說明這一點,再要求使用者同意。
只有在使用者明確表示要使用需要該範圍的特定功能時,您才可以再次提示使用者。在要求 OAuth 範圍前,應用程式應先向使用者提供相關背景資訊和理由。
請盡量減少應用程式一次要求的範圍數量。而是利用漸進式授權,在功能和功能的背景資訊中要求權限範圍。
使用安全的瀏覽器
在網路上,OAuth 2.0 授權要求只能透過功能完整的網路瀏覽器提出。在其他平台上,請務必選取正確的 OAuth 用戶端類型,並視平台需求整合 OAuth。請勿透過嵌入式瀏覽環境重新導向要求,包括行動平台上的網頁檢視畫面,例如 Android 上的 WebView 或 iOS 上的 WKWebView。請改用原生 OAuth 程式庫或平台的 Google 登入。
手動建立及設定 OAuth 用戶端
為避免濫用,您無法透過程式碼建立或修改 OAuth 用戶端。您必須使用 Google 開發人員控制台明確確認服務條款、設定 OAuth 用戶端,並準備進行 OAuth 驗證。
如果是自動化工作流程,建議改用服務帳戶。
移除未使用的 OAuth 用戶端
定期稽核 OAuth 2.0 用戶端,並主動刪除應用程式不再需要或已淘汰的用戶端。保留未使用的用戶端會帶來潛在的安全風險,因為如果用戶端憑證遭到盜用,用戶端就可能遭到濫用。
為進一步降低未使用的用戶端所帶來的風險,系統會 自動刪除閒置六個月的 OAuth 2.0 用戶端。
建議的最佳做法是不要等待系統自動刪除,而是主動移除未使用的用戶端。這種做法可盡量減少應用程式的攻擊面,並確保良好的安全衛生。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eSecurely store and manage OAuth client credentials, avoiding hardcoding or public exposure.\u003c/p\u003e\n"],["\u003cp\u003eProtect user tokens (refresh and access) by storing them securely and revoking them when no longer needed.\u003c/p\u003e\n"],["\u003cp\u003eImplement proper handling of refresh token revocation and expiration scenarios, including user notification and data cleanup.\u003c/p\u003e\n"],["\u003cp\u003eUtilize incremental authorization to request only necessary OAuth scopes in context, minimizing initial requests and enhancing user privacy.\u003c/p\u003e\n"],["\u003cp\u003eEmploy secure browsing environments for OAuth authorization requests, avoiding embedded browsers like webviews and opting for native libraries or Google Sign-in.\u003c/p\u003e\n"]]],[],null,["This page covers some general best practices for integrating with OAuth 2.0. Consider these best\npractices in addition to any specific guidance for your type of application and development\nplatform. Also refer to the\n[advice for getting\nyour app ready for production](/identity/protocols/oauth2/production-readiness/policy-compliance) and [Google's\nOAuth 2.0 policies](/identity/protocols/oauth2/policies).\n\nHandle client credentials securely\n\n\nThe OAuth client credentials identify your app's identity and should be handled carefully. Only\nstore these credentials in secure storage, for example using a secret manager such as\n[Google Cloud Secret Manager](https://cloud.google.com/secret-manager/docs/overview).\nDo not hardcode the credentials, commit them to a code repository or publish them publicly.\n\nHandle user tokens securely\n\n\nUser tokens include both refresh tokens and access tokens used by your application. Store\ntokens securely [at rest](https://wikipedia.org/wiki/Data_at_rest)\nand never transmit them in plain text. Use a secure storage system appropriate for your\nplatform, such as\n[Keystore](https://developer.android.com/training/articles/keystore) on Android,\nKeychain Services on iOS and macOS, or Credential Locker on Windows.\n\n\n[Revoke tokens](/identity/protocols/oauth2/web-server#tokenrevoke) as soon as they\nare no longer needed and delete them permanently from your systems.\n\n\nIn addition, also consider these best practices for your platform:\n\n- For [server-side](/identity/protocols/oauth2/web-server) applications that store tokens for many users, encrypt them at rest and ensure that your data store is not publicly accessible to the Internet.\n- For native desktop apps, using the [Proof Key for Code\n Exchange (PKCE) protocol](/identity/protocols/oauth2/native-app#obtainingaccesstokens) is strongly recommended to obtain authorization codes that can be exchanged for access tokens.\n\nHandle refresh token revocation and expiration\n\n\nIf your app has requested a [refresh\ntoken for offline access](/identity/protocols/oauth2/web-server#offline), you must also handle their invalidation or expiration. Tokens\ncould be [invalidated for different reasons](/identity/protocols/oauth2#expiration),\nfor example it could have expired or your apps' access could have been revoked by the user or\nan automated process. In this case, consider carefully how your application should respond,\nincluding prompting the user at their next log in or cleaning up their data. To be notified of\ntoken revocation, integrate with the [Cross-Account\nProtection](/identity/protocols/risc) service.\n\nUse incremental authorization\n\n\nUse [incremental\nauthorization](/identity/protocols/oauth2/web-server#incrementalAuth) to request appropriate OAuth scopes when the functionality is needed by your\napplication.\n\n\nYou should not request access to data when the user first authenticates, unless it is essential\nfor the core functionality of your app. Instead, request only the specific scopes that are\nneeded for a task, following the principle to\n[select the smallest, most limited scopes possible](/identity/protocols/oauth2/production-readiness/policy-compliance#only-request-needed-scopes).\n\n\nAlways request scopes in context to help your users understand why your app is requesting access\nand how the data will be used.\n\n\nFor example, your application may follow this model:\n\n1. The user authenticates with your app\n 1. No additional scopes are requested. The app provides basic functionality to let the user explore and use features that do not require any additional data or access.\n2. The user selects a feature that requires access to additional data\n 1. Your application makes an authorization request for this specific OAuth scope required for this feature. If this feature requires multiple scopes, follow [the best practices below](#multiple-scopes).\n 2. If the user denies the request, the app disables the feature and gives the user additional context to request access again.\n\nHandle consent for multiple scopes\n\n\nWhen requesting multiple scopes at once, users may not grant all OAuth scopes you have\nrequested. Your app should handle the denial of scopes by disabling relevant functionality.\n\n\nIf your app's basic functionality requires multiple scopes, explain this to the user before\nprompting for consent.\n\n\nYou may only prompt the user again once they have clearly indicated an intent to use the\nspecific feature that requires the scope. Your app should provide the user with relevant context\nand justification before requesting OAuth scopes.\n\n\nYou should minimize the number of scopes your app requests at once. Instead,\n[utilize incremental authorization](#use-incremental-authorization) to request scopes\nin context of features and functionality.\n\nUse secure browsers\n\n\nOn the web, OAuth 2.0 authorization requests must only be made from full-featured web browsers.\nOn other platforms, make sure to select the\n[correct OAuth client type](/identity/protocols/oauth2#basicsteps) and integrate\nOAuth as appropriate for your platform. Do not redirect the request through embedded browsing\nenvironments, including webviews on mobile platforms, such as WebView on Android or WKWebView on\niOS. Instead, utilize [native OAuth libraries](/identity/protocols/oauth2/native-app)\nor [Google Sign-in](/identity/authorization) for your platform.\n\nManual creation and configuration of OAuth clients\n\n\nIn order to prevent abuse, OAuth clients cannot be created or modified programmatically. You\nmust use the Google Developers console to explicitly acknowledge the terms of service, configure\nyour OAuth client and prepare for OAuth verification.\n\n\nFor automated workflows, consider using\n[service accounts](/identity/protocols/oauth2/service-account) instead.\n\nRemove unused OAuth clients\n\n\nRegularly audit your OAuth 2.0 clients and proactively delete any that are no longer required by\nyour application or have become obsolete. Leaving unused clients configured represents a\npotential security risk as the client can be misused if your client credentials are ever\ncompromised.\n\n\nTo further mitigate risks from unused clients, OAuth 2.0 clients that have been inactive for six\nmonths are [automatically deleted](https://support.google.com/cloud/answer/15549257#unused-client-deletion).\n\n\nThe recommended best practice is to not wait for automatic deletion but rather proactively\nremove unused clients. This practice minimizes your application's attack surface and ensures\ngood security hygiene."]]