最佳实践
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页介绍了一些与 OAuth 2.0 集成的一般最佳实践。除了针对您的应用类型和开发平台的任何具体指导之外,还应考虑以下最佳实践。另请参阅有关准备将应用用于生产环境的建议和 Google 的 OAuth 2.0 政策。
安全地处理用户令牌
用户令牌包括您的应用使用的刷新令牌和访问令牌。安全地存储静态令牌,并且绝不以纯文本形式传输令牌。使用适合您平台的安全存储系统,例如 Android 上的 Keystore、iOS 和 macOS 上的密钥链服务,或 Windows 上的凭据保险箱。
在不再需要令牌时,立即撤消令牌,并从您的系统中永久删除令牌。
此外,还请考虑以下平台最佳实践:
处理刷新令牌撤消和过期
如果您的应用已请求用于离线访问的刷新令牌,您还必须处理其失效或过期情况。令牌可能会因各种原因而失效,例如令牌可能已过期,或者用户或自动化流程可能已撤消应用的访问权限。在这种情况下,请仔细考虑您的应用应如何响应,包括在用户下次登录时提示用户或清理用户的数据。如需在令牌被撤消时收到通知,请与跨账号保护服务集成。
使用增量授权
使用增量授权在应用需要相应功能时请求适当的 OAuth 范围。
除非对应用的核心功能至关重要,否则您不应在用户首次进行身份验证时请求访问数据。相反,您应仅请求任务所需的特定范围,并遵循尽可能选择最小、最有限的范围的原则。
始终在用户执行相关操作时请求范围,以帮助用户了解您的应用为何请求访问权限以及数据将如何使用。
例如,您的应用可以遵循以下模型:
- 用户向您的应用进行身份验证
- 未请求任何其他范围。该应用提供基本功能,让用户探索和使用不需要任何额外数据或访问权限的功能。
- 用户选择需要访问其他数据的功能
- 您的应用会针对此功能所需的特定 OAuth 范围发出授权请求。如果此功能需要多个范围,请遵循以下最佳实践。
- 如果用户拒绝该请求,应用会停用相应功能,并向用户提供更多背景信息,以便用户再次请求访问权限。
处理针对多个范围的用户意见征求
当您同时请求多个范围时,用户可能不会授予您请求的所有 OAuth 范围。应用应通过停用相关功能来处理范围遭拒的情况。
如果应用的基本功能需要多个范围,请在提示用户征求同意之前向用户说明这一点。
只有在用户明确表示有意使用需要相应范围的特定功能时,您才可以再次提示用户。在请求 OAuth 范围之前,您的应用应向用户提供相关背景信息和理由。
您应尽量减少应用一次请求的范围数量。请改为利用增量授权,在功能和功能区的上下文中请求相应范围。
使用安全的浏览器
在 Web 上,OAuth 2.0 授权请求只能通过功能齐全的 Web 浏览器发出。
在其他平台上,请务必选择正确的 OAuth 客户端类型,并根据您的平台集成 OAuth。请勿通过嵌入式浏览环境(包括移动平台上的 WebView,例如 Android 上的 WebView 或 iOS 上的 WKWebView)重定向请求。请改为使用平台专用的原生 OAuth 库或 Google 登录。
手动创建和配置 OAuth 客户端
为防止滥用,无法以编程方式创建或修改 OAuth 客户端。您必须使用 Google Developers 控制台明确确认服务条款,配置 OAuth 客户端并为 OAuth 验证做好准备。
对于自动化工作流,请考虑改用服务账号。
移除未使用的 OAuth 客户端
定期审核 OAuth 2.0 客户端,并主动删除应用不再需要或已过时的客户端。如果未使用的客户端处于已配置状态,则存在潜在的安全风险,因为如果您的客户端凭据遭到入侵,该客户端可能会被滥用。
为了进一步降低未使用客户端带来的风险,系统会
自动删除已闲置六个月的 OAuth 2.0 客户端。
建议的最佳做法是不要等待自动删除,而是主动移除未使用的客户端。这种做法可最大限度地缩小应用的受攻击面,并确保良好的安全卫生。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\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."]]