ベスト プラクティス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このページでは、OAuth 2.0 との統合に関する一般的なベスト プラクティスについて説明します。アプリケーションの種類と開発プラットフォームに関する具体的なガイダンスに加えて、次のベスト プラクティスを検討してください。アプリを本番環境向けに準備するためのアドバイスと Google の OAuth 2.0 に関するポリシーもご覧ください。
クライアント認証情報を安全に処理する
OAuth クライアント認証情報はアプリの ID を識別するものであり、慎重に処理する必要があります。これらの認証情報は、Google Cloud Secret Manager などのシークレット マネージャーを使用して、安全なストレージにのみ保存します。認証情報をハードコードしたり、コード リポジトリにコミットしたり、一般公開したりしないでください。
ユーザー トークンを安全に処理する
ユーザー トークンには、アプリケーションで使用される更新トークンとアクセス トークンの両方が含まれます。トークンを安全に保存し、平文で送信しないでください。Android の Keystore、iOS と macOS の Keychain Services、Windows の Credential Locker など、プラットフォームに適した安全なストレージ システムを使用します。
トークンが不要になったらすぐにトークンを取り消し、システムから完全に削除します。
また、プラットフォームのベスト プラクティスも考慮してください。
更新トークンの取り消しと有効期限切れを処理する
アプリがオフライン アクセス用の更新トークンをリクエストしている場合は、その無効化または有効期限切れも処理する必要があります。トークンは、期限切れ、ユーザーまたは自動プロセスによるアプリのアクセス権の取り消しなど、さまざまな理由で無効になる可能性があります。この場合は、次回のログイン時にユーザーにプロンプトを表示する、データをクリーンアップするなど、アプリケーションがどのように応答すべきかを慎重に検討してください。トークンの取り消しに関する通知を受け取るには、クロスアカウント保護サービスと統合します。
段階的な認可を使用する
アプリケーションで機能が必要になったときに、適切な OAuth スコープをリクエストするには、増分承認を使用します。
アプリのコア機能に不可欠な場合を除き、ユーザーが最初に認証するときにデータへのアクセスをリクエストしないでください。代わりに、タスクに必要な特定のスコープのみをリクエストし、可能な限り最小かつ最も制限されたスコープを選択するという原則に従ってください。
アプリがアクセスをリクエストする理由とデータの使用方法をユーザーが理解できるように、常にコンテキスト内でスコープをリクエストします。
たとえば、アプリケーションは次のモデルに従うことができます。
- ユーザーがアプリで認証する
- 追加のスコープはリクエストされません。アプリは、追加のデータやアクセスを必要としない機能をユーザーが探索して使用できるようにする基本機能を提供します。
- ユーザーが追加データへのアクセスを必要とする機能を選択します。
- アプリケーションが、この機能に必要な特定の OAuth スコープの認可リクエストを行います。この機能に複数のスコープが必要な場合は、以下のベスト プラクティスに沿って対応してください。
- ユーザーがリクエストを拒否すると、アプリは機能を無効にし、ユーザーが再度アクセスをリクエストするための追加のコンテキストを提供します。
複数のスコープの同意を処理する
複数のスコープを一度にリクエストすると、ユーザーがリクエストされたすべての OAuth スコープを許可しない可能性があります。アプリは、関連する機能を無効にすることで、スコープの拒否を処理する必要があります。
アプリの基本機能に複数のスコープが必要な場合は、同意を求める前にユーザーに説明してください。
ユーザーがスコープを必要とする特定の機能を使用する意思を明確に示した場合にのみ、ユーザーに再度プロンプトを表示できます。アプリは、OAuth スコープをリクエストする前に、関連するコンテキストと理由をユーザーに提供する必要があります。
アプリが一度にリクエストするスコープの数は最小限に抑える必要があります。代わりに、増分認証を利用して、機能のコンテキストでスコープをリクエストします。
安全なブラウザを使用する
ウェブでは、OAuth 2.0 認証リクエストはフル機能のウェブブラウザからのみ行う必要があります。他のプラットフォームでは、適切な OAuth クライアント タイプを選択し、プラットフォームに応じて OAuth を統合してください。Android の WebView や iOS の WKWebView などのモバイル プラットフォームの WebView を含む、埋め込みブラウジング環境を介してリクエストをリダイレクトしないでください。代わりに、プラットフォームのネイティブ OAuth ライブラリまたは Google ログインを使用してください。
OAuth クライアントの手動作成と構成
不正使用を防ぐため、OAuth クライアントをプログラムで作成または変更することはできません。Google Developers コンソールを使用して、利用規約に明示的に同意し、OAuth クライアントを構成して、OAuth 認証の準備を行う必要があります。
自動化されたワークフローでは、代わりにサービス アカウントの使用を検討してください。
未使用の OAuth クライアントを削除する
OAuth 2.0 クライアントを定期的に監査し、アプリケーションで不要になったものや廃止されたものを事前に削除します。未使用のクライアントが構成されたままになっていると、クライアント認証情報が漏洩した場合にクライアントが不正使用される可能性があるため、セキュリティ リスクが生じます。
未使用のクライアントによるリスクをさらに軽減するため、6 か月間アクティブでない OAuth 2.0 クライアントは
自動的に削除されます。
推奨されるベスト プラクティスは、自動削除を待つのではなく、使用されていないクライアントを事前に削除することです。このプラクティスにより、アプリケーションの攻撃対象領域が最小限に抑えられ、セキュリティ対策が徹底されます。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-31 UTC。
[null,null,["最終更新日 2025-08-31 UTC。"],[[["\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."]]