أفضل الممارسات
تنظيم صفحاتك في مجموعات يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تتناول هذه الصفحة بعض أفضل الممارسات العامة للدمج مع OAuth 2.0. ننصحك باتّباع أفضل الممارسات التالية بالإضافة إلى أي إرشادات محدّدة لنوع تطبيقك والمنصة التي تستخدمها لتطويره. يُرجى أيضًا الاطّلاع على النصائح المتعلقة بإعداد تطبيقك للإصدار العلني وسياسات OAuth 2.0 في Google.
التعامل مع بيانات اعتماد العميل بأمان
تحدِّد بيانات اعتماد عميل OAuth هوية تطبيقك، ويجب التعامل معها بعناية. تخزين بيانات الاعتماد هذه في مساحة تخزين آمنة فقط، على سبيل المثال باستخدام أداة إدارة الأسرار مثل Google Cloud Secret Manager لا تُدرج بيانات الاعتماد في الرمز البرمجي ولا تُرسِلها إلى مستودع رموز أو تنشرها علنًا.
التعامل مع الرموز المميّزة للمستخدمين بأمان
تشمل الرموز المميّزة للمستخدمين كلّ من الرموز المميّزة لإعادة التحميل والرموز المميّزة للوصول التي يستخدمها تطبيقك. تخزين الرموز المميّزة بأمان في وضع عدم الاتّصال بالإنترنت وعدم إرسالها مطلقًا كنص عادي استخدِم نظام تخزين آمنًا مناسبًا للنظام الأساسي، مثل Keystore على Android، أو Keychain Services على iOS وmacOS، أو Credential Locker على Windows.
إبطال الرموز المميّزة فور عدم الحاجة إليها وحذفها نهائيًا من أنظمتك
بالإضافة إلى ذلك، ننصحك باتّباع أفضل الممارسات التالية في منصتك:
- بالنسبة إلى التطبيقات التي تعمل من جهة الخادم وتخزِّن الرموز المميّزة للعديد من المستخدمين، يجب تشفيرها في وضع عدم الاتّصال بالإنترنت والتأكّد من أنّه لا يمكن للجميع الوصول إلى قاعدة بياناتك.
- بالنسبة إلى تطبيقات سطح المكتب الأصلية، ننصح بشدة باستخدام بروتوكول مفتاح الحماية لتبادل الرموز (PKCE) للحصول على رموز التفويض التي يمكن تبديلها برموز الوصول.
التعامل مع إبطال الرمز المميّز لإعادة التحميل وانتهاء صلاحيته
إذا طلب تطبيقك رمزًا مميزًا لإعادة التحديث للوصول بلا إنترنت، عليك أيضًا معالجة حالات إبطاله أو انتهاء صلاحيته. يمكن أن يتم إبطال صحة الرموز المميّزة لأسباب مختلفة، مثلاً بسبب انتهاء صلاحيتها أو إذا أبطل المستخدم أو عملية مبرمَجة إذن وصول تطبيقاتك إليها. في هذه الحالة، يجب التفكير بعناية في كيفية استجابة تطبيقك، بما في ذلك مطالبة المستخدم بتنظيف بياناته عند تسجيل الدخول التالي. لتلقّي إشعارات بشأن إبطال الرمز المميّز، يمكنك الدمج مع خدمة الحماية العابرة للحساب.
استخدام التفويض المتزايد
استخدِم التفويض المتصاعدلطلب نطاقات OAuth المناسبة عندما يحتاج تطبيقك إلى الوظيفة.
يجب عدم طلب الوصول إلى البيانات عند مصادقة المستخدم لأول مرة، ما لم يكن ذلك ضروريًا للوظائف الأساسية لتطبيقك. بدلاً من ذلك، اطلب النطاقات المحدّدة فقط التي تحتاج إليها لمهمة معيّنة، مع اتّباع مبدأ اختيار النطاقات الأصغر حجمًا والأكثر تقييدًا.
اطلب النطاقات دائمًا في سياق استخدامها لمساعدة المستخدمين في فهم سبب طلب تطبيقك للوصول إلى البيانات وكيفية استخدامها.
على سبيل المثال، قد يتّبع تطبيقك النموذج التالي:
- يُثبِّت المستخدم هويته باستخدام تطبيقك
- ولا يُطلب أيّ نطاقات إضافية. يقدّم التطبيق وظائف أساسية للسماح للمستخدم باستكشاف الميزات واستخدامها التي لا تتطلّب أي بيانات أو أذونات إضافية.
- يختار المستخدم ميزة تتطلّب الوصول إلى بيانات إضافية
- يقدّم تطبيقك طلب تفويض لنطاق OAuth المحدّد هذا المطلوب لهذه الميزة. إذا كانت هذه الميزة تتطلّب نطاقات متعددة، اتّبِع أفضل الممارسات أدناه.
- إذا رفض المستخدم الطلب، يوقف التطبيق الميزة ويقدّم للمستخدم سياقًا إضافيًا لطلب الوصول مرة أخرى.
معالجة الموافقة على نطاقات متعددة
عند طلب نطاقات متعددة في آنٍ واحد، قد لا يمنح المستخدمون جميع نطاقات OAuth التي طلبتها. يجب أن يتعامل تطبيقك مع رفض النطاقات من خلال إيقاف الوظائف ذات الصلة.
إذا كانت الوظيفة الأساسية لتطبيقك تتطلّب نطاقات متعددة، يجب توضيح ذلك للمستخدم قبل طلب موافقته.
لا يجوز لك مطالبة المستخدم مرة أخرى إلا بعد أن يشير بوضوح إلى رغبته في استخدام الميزة المحدّدة التي تتطلّب النطاق. يجب أن يقدّم تطبيقك للمستخدم سياقًا مناسبًا وسببًا وجيهًا قبل طلب نطاقات OAuth.
عليك تقليل عدد النطاقات التي يطلبها تطبيقك في آنٍ واحد. بدلاً من ذلك، استخدِم المصادقة المتزايدة لطلب النطاقات في سياق الميزات والوظائف.
استخدام متصفّحات آمنة
على الويب، يجب تقديم طلبات التفويض باستخدام بروتوكول OAuth 2.0 من متصفّحات الويب المزوّدة بميزات كاملة فقط. على المنصات الأخرى، احرص على اختيار نوع عميل OAuth الصحيح ودمج OAuth بما يناسب منصتك. لا تُعِد توجيه الطلب من خلال بيئات التصفّح المضمّنة، بما في ذلك WebView على الأنظمة الأساسية للأجهزة الجوّالة، مثل WebView على Android أو WKWebView على iOS. بدلاً من ذلك، استخدِم مكتبات OAuth الأصلية أو تسجيل الدخول باستخدام حساب Google لمنصتك.
الإنشاء اليدوي لعملاء OAuth وإعدادهم
لمنع إساءة الاستخدام، لا يمكن إنشاء برامج OAuth أو تعديلها آليًا. عليك استخدام "وحدة تحكّم مطوّري تطبيقات Google" للإقرار صراحةً ببنود الخدمة وضبط برنامج OAuth والاستعداد لإثبات ملكية حساب OAuth.
بالنسبة إلى سير العمل المبرمَج، ننصحك باستخدام حسابات الخدمة بدلاً من ذلك.
إزالة عملاء OAuth غير المستخدَمين
تحقَّق بانتظام من عملاء OAuth 2.0 واحذِف بشكل استباقي أي عملاء لم يعد تطبيقك يحتاجهم أو أصبحوا قديمًا. يشكّل ترك عملاء غير مستخدَمين مُعدَّين خطرًا محتمَلاً على الأمان، لأنّه يمكن إساءة استخدام العميل في حال تم اختراق بيانات اعتماد العميل.
للحدّ من المخاطر الناتجة عن العملاء غير المستخدَمين، يتم تلقائيًا حذف عملاء OAuth 2.0 الذين ظلّوا غير نشطين لمدة ستة أشهر.
أفضل الممارسات المقترَحة هي عدم الانتظار إلى أن يتم الحذف التلقائي، بل إزالة العملاء غير المستخدَمين بشكل استباقي. تقلِّل هذه الممارسة من مساحة الهجوم التي يتعرض لها تطبيقك وتؤمّن إجراءات أمان جيدة.
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 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."]]