تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تتضمّن خدمة SafetyNet واجهة برمجة تطبيقات reCAPTCHA يمكنك استخدامها لحماية تطبيقك من الزيارات الضارة.
reCAPTCHA هي خدمة مجانية تستخدم محركًا متقدمًا لتحليل المخاطر من أجل حماية تطبيقك من الرسائل غير المرغوب فيها والإجراءات المسيئة الأخرى. إذا اشتبهت الخدمة في أنّ المستخدم الذي يتفاعل مع تطبيقك قد يكون برنامج تتبُّع بدلاً من مستخدم فعلي، ستعرض اختبار CAPTCHA يجب أن يجتازه المستخدم الفعلي قبل أن يتمكّن تطبيقك من مواصلة التنفيذ.
يوضّح هذا المستند كيفية دمج reCAPTCHA API من SafetyNet في تطبيقك.
بنود الخدمة الإضافية
إذا وصلت إلى واجهة برمجة التطبيقات reCAPTCHA أو استخدمتها، يعني ذلك أنّك توافق على بنود خدمة Google APIs وعلى بنود خدمة reCAPTCHA التالية.
يُرجى قراءة جميع الأحكام والسياسات السارية وفهمها قبل الوصول إلى واجهات برمجة التطبيقات.
بنود خدمة reCAPTCHA
أنت تُقر وتفهم أنّ واجهة برمجة تطبيقات reCAPTCHA تعمل عن طريق جمع معلومات عن الأجهزة والبرامج، مثل بيانات الجهاز والتطبيق ونتائج عمليات التحقّق من التكامل، ثم إرسال هذه البيانات إلى Google للتحليل.
وبموجب الفقرة(د) من القسم 3 من "بنود خدمة Google APIs"، أنت توافق على أنّه في حال استخدمت واجهات برمجة التطبيقات، تقع عليك مسؤولية توفير أي إشعارات أو موافقات لازمة لجمع مثل هذه البيانات ومشاركتها مع Google.
التصنيف: تصنيف فريد لمفتاحك. عادةً ما تستخدم اسم شركتك أو مؤسستك.
نوع reCAPTCHA: اختَر الإصدار الثاني من خدمة reCAPTCHA، ثم reCAPTCHA على Android.
الحِزم: أدخِل اسم حزمة كل تطبيق يستخدم مفتاح واجهة برمجة التطبيقات هذا. لكي يتمكّن تطبيق من استخدام واجهة برمجة التطبيقات، يجب أن يتطابق اسم الحزمة الذي تدخله تمامًا مع اسم حزمة التطبيق. أدخِل كل اسم حزمة في سطر منفصل.
المالكون: أضِف عنوان بريد إلكتروني لكل فرد في مؤسستك
يراقب تقييمات reCAPTCHA لتطبيقك.
ضَع علامة في مربّع الاختيار قبول بنود خدمة reCAPTCHA.
إرسال تنبيهات إلى المالكين: ضَع علامة في مربّع الاختيار هذا إذا كنت تريد تلقّي رسائل إلكترونية حول reCAPTCHA API، ثم انقر على إرسال.
قبل استخدام reCAPTCHA API، أضِف SafetyNet API إلى مشروعك.
إذا كنت تستخدم "استوديو Android"، أضِف هذه التبعية إلى ملف Gradle على مستوى التطبيق. لمزيد من المعلومات، يُرجى الاطّلاع على إعداد SafetyNet API.
استخدام واجهة برمجة تطبيقات reCAPTCHA
يوضّح هذا القسم كيفية طلب بيانات من واجهة برمجة تطبيقات reCAPTCHA لإرسال طلب تأكيد CAPTCHA وتلقّي رمز استجابة المستخدم.
إرسال طلب التحقّق
لاستدعاء واجهة برمجة تطبيقات SafetyNet reCAPTCHA، عليك استدعاء طريقة
verifyWithRecaptcha(). يتوافق هذا الإجراء عادةً مع اختيار المستخدم لعنصر واجهة مستخدم، مثل زر، في نشاطك.
عند استخدام طريقة
verifyWithRecaptcha()
في تطبيقك، عليك اتّخاذ الإجراءات التالية:
مرِّر مفتاح الموقع الإلكتروني لواجهة برمجة التطبيقات كمَعلمة.
تجاوز الطريقتَين onSuccess()
وonFailure()
للتعامل مع النتيجتَين المحتملتَين لمهمة طلب التحقّق. على وجه الخصوص، إذا كانت واجهة برمجة التطبيقات تمرّر مثيلاً من ApiException إلى onFailure()، عليك التعامل مع كل رمز حالة محتمل يمكنك استرداده باستخدام getStatusCode().
يوضّح مقتطف الرمز البرمجي التالي كيفية استدعاء هذه الطريقة:
Kotlin
funonClick(view:View){SafetyNet.getClient(this).verifyWithRecaptcha(YOUR_API_SITE_KEY).addOnSuccessListener(thisasExecutor,OnSuccessListener{response->// Indicates communication with reCAPTCHA service was// successful.valuserResponseToken=response.tokenResultif(response.tokenResult?.isNotEmpty()==true){// Validate the user response token using the// reCAPTCHA siteverify API.}}).addOnFailureListener(thisasExecutor,OnFailureListener{e->if(eisApiException){// An error occurred when communicating with the// reCAPTCHA service. Refer to the status code to// handle the error appropriately.Log.d(TAG,"Error: ${CommonStatusCodes.getStatusCodeString(e.statusCode)}")}else{// A different, unknown type of error occurred.Log.d(TAG,"Error: ${e.message}")}})}
Java
publicvoidonClick(Viewv){SafetyNet.getClient(this).verifyWithRecaptcha(YOUR_API_SITE_KEY).addOnSuccessListener((Executor)this,newOnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>(){@OverridepublicvoidonSuccess(SafetyNetApi.RecaptchaTokenResponseresponse){// Indicates communication with reCAPTCHA service was// successful.StringuserResponseToken=response.getTokenResult();if(!userResponseToken.isEmpty()){// Validate the user response token using the// reCAPTCHA siteverify API.}}}).addOnFailureListener((Executor)this,newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){if(einstanceofApiException){// An error occurred when communicating with the// reCAPTCHA service. Refer to the status code to// handle the error appropriately.ApiExceptionapiException=(ApiException)e;intstatusCode=apiException.getStatusCode();Log.d(TAG,"Error: "+CommonStatusCodes.getStatusCodeString(statusCode));}else{// A different, unknown type of error occurred.Log.d(TAG,"Error: "+e.getMessage());}}});}
التحقّق من صحة الرمز المميّز لاستجابة المستخدم
عندما تنفّذ واجهة برمجة التطبيقات reCAPTCHA طريقة
onSuccess()
، يكون المستخدم قد أكمل تحدّي CAPTCHA بنجاح. ومع ذلك، لا تشير هذه الطريقة إلا إلى أنّ المستخدم قد حلّ اختبار CAPTCHA بشكل صحيح. سيظل عليك التحقّق من صحة الرمز المميّز الخاص باستجابة المستخدم من الخادم الخلفي.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-09-04 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-09-04 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["| **Warning:** In order to help you safely grow your business, Google builds tools to protect your Android apps and games from abuse. We evolve these solutions as the abuse landscape changes. Delivering on this promise, Google is replacing the SafetyNet reCAPTCHA API with [reCAPTCHA](https://cloud.google.com/recaptcha/docs/instrument-android-apps). reCAPTCHA offers superior protection for mobile applications. We plan to gradually turn down the SafetyNet reCAPTCHA API starting in the third quarter of 2025. Details can be found in the [reCAPTCHA deprecation and shutdown policy](https://cloud.google.com/recaptcha/docs/deprecation-policy-mobile).\n\nThe SafetyNet service includes a reCAPTCHA API that you can use to protect your\napp from malicious traffic.\n\nreCAPTCHA is a free service that uses an advanced risk analysis engine to\nprotect your app from spam and other abusive actions. If the service suspects\nthat the user interacting with your app might be a bot instead of a human, it\nserves a CAPTCHA that a human must solve before your app can continue executing.\n| **Note:** In order to use this API, your app must set `minSdkVersion` to `14` or higher in the `AndroidManifest.xml` file.\n\nThis document explains how to integrate the reCAPTCHA API from SafetyNet into\nyour app.\n\nAdditional terms of service\n\nBy accessing or using the reCAPTCHA API, you agree to the [Google APIs Terms of\nService](https://developers.google.com/terms/) and to the following reCAPTCHA\nTerms of Service.\nPlease read and understand all applicable terms and policies before accessing\nthe APIs. \n\nreCAPTCHA Terms of Service You acknowledge and understand that the reCAPTCHA API works by collecting hardware and software information, such as device and application data and the results of integrity checks, and sending that data to Google for analysis. Pursuant to Section 3(d) of the Google APIs Terms of Service, you agree that if you use the APIs that it is your responsibility to provide any necessary notices or consents for the collection and sharing of this data with Google.\n\nRegister a reCAPTCHA key pair\n\nTo register a key pair for use with the SafetyNet reCAPTCHA API, navigate to the\n[reCAPTCHA Android signup site](https://www.google.com/recaptcha/admin/create),\nthen complete the following sequence of steps:\n\n1. In the form that appears, provide the following information:\n\n - **Label:** A unique label for your key. Typically, you use the name of your company or organization.\n - **reCAPTCHA type:** Select **reCAPTCHA v2** , then **reCAPTCHA Android**.\n - **Packages:** Provide the package name of each app that uses this API key. In order for an app to use the API, the package name that you enter must exactly match the app's package name. Enter each package name on its own line.\n - **Owners:** Add an email address for each individual in your organization who monitors your app's reCAPTCHA assessments.\n2. Select the **Accept the reCAPTCHA Terms of Service** checkbox.\n\n3. **Send alerts to owners:** Select this checkbox if you want to receive\n emails about the reCAPTCHA API, then click\n **Submit**.\n\n4. On the page that appears\n next, your public and private keys appear under **Site key** and **Secret\n key** , respectively. You use the site key when you [send the verify\n request](#send-request), and you use the secret key when you [validate the\n user response token](#validate-response).\n\nAdd the SafetyNet API dependency\n\nBefore using the reCAPTCHA API, add the SafetyNet API to your project.\nIf you are using Android Studio, add this dependency to your app-level Gradle\nfile. For more information, see [SafetyNet API setup](https://developer.android.com/training/safetynet#before-you-begin).\n\nUse the reCAPTCHA API\n\nThis section describes how to call the reCAPTCHA API to send a CAPTCHA\nverification request and receive the user response token.\n\nSend the verify request\n\nTo invoke the SafetyNet reCAPTCHA API, you call the\n[`verifyWithRecaptcha()`](https://developers.google.com/android/reference/com/google/android/gms/safetynet/SafetyNetClient.html#verifyWithRecaptcha(java.lang.String))\nmethod. Usually, this method corresponds to the user's selecting a UI element,\nsuch as a button, in your activity.\n\nWhen using the\n`verifyWithRecaptcha()`\nmethod in your app, you must do the following:\n\n- Pass in your API site key as a parameter.\n- Override the [`onSuccess()`](https://developers.google.com/android/reference/com/google/android/gms/tasks/OnSuccessListener.html#onSuccess(TResult)) and [`onFailure()`](https://developers.google.com/android/reference/com/google/android/gms/tasks/OnFailureListener.html#onFailure(java.lang.Exception)) methods to handle both possible outcomes of the verification request task. In particular, if the API passes an instance of [`ApiException`](https://developers.google.com/android/reference/com/google/android/gms/common/api/ApiException) into `onFailure()`, you need to handle each possible status code that you can retrieve using [`getStatusCode()`](https://developers.google.com/android/reference/com/google/android/gms/common/api/ApiException#getStatusCode()).\n\nThe following code snippet shows how to invoke this method: \n\nKotlin \n\n```kotlin\nfun onClick(view: View) {\n SafetyNet.getClient(this).verifyWithRecaptcha(YOUR_API_SITE_KEY)\n .addOnSuccessListener(this as Executor, OnSuccessListener { response -\u003e\n // Indicates communication with reCAPTCHA service was\n // successful.\n val userResponseToken = response.tokenResult\n if (response.tokenResult?.isNotEmpty() == true) {\n // Validate the user response token using the\n // https://developers.google.com/recaptcha/docs/verify.\n }\n })\n .addOnFailureListener(this as Executor, OnFailureListener { e -\u003e\n if (e is ApiException) {\n // An error occurred when communicating with the\n // reCAPTCHA service. https://developers.google.com/android/reference/com/google/android/gms/safetynet/SafetyNetStatusCodes to\n // handle the error appropriately.\n Log.d(TAG, \"Error: ${CommonStatusCodes.getStatusCodeString(e.statusCode)}\")\n } else {\n // A different, unknown type of error occurred.\n Log.d(TAG, \"Error: ${e.message}\")\n }\n })\n}\n```\n\nJava \n\n```java\npublic void onClick(View v) {\n SafetyNet.getClient(this).verifyWithRecaptcha(YOUR_API_SITE_KEY)\n .addOnSuccessListener((Executor) this,\n new OnSuccessListener\u003cSafetyNetApi.RecaptchaTokenResponse\u003e() {\n @Override\n public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {\n // Indicates communication with reCAPTCHA service was\n // successful.\n String userResponseToken = response.getTokenResult();\n if (!userResponseToken.isEmpty()) {\n // Validate the user response token using the\n // https://developers.google.com/recaptcha/docs/verify.\n }\n }\n })\n .addOnFailureListener((Executor) this, new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception e) {\n if (e instanceof ApiException) {\n // An error occurred when communicating with the\n // reCAPTCHA service. https://developers.google.com/android/reference/com/google/android/gms/safetynet/SafetyNetStatusCodes to\n // handle the error appropriately.\n ApiException apiException = (ApiException) e;\n int statusCode = apiException.getStatusCode();\n Log.d(TAG, \"Error: \" + CommonStatusCodes\n .getStatusCodeString(statusCode));\n } else {\n // A different, unknown type of error occurred.\n Log.d(TAG, \"Error: \" + e.getMessage());\n }\n }\n });\n}\n```\n\nValidate the user response token\n\nWhen the reCAPTCHA API executes the\n`onSuccess()`\nmethod, the user has successfully completed the CAPTCHA challenge. However, this\nmethod only indicates that the user has solved the CAPTCHA correctly. You still\nneed to validate the user's response token from your backend server.\n\nTo learn how to validate the user's response token, see [Verifying the user's\nresponse](https://developers.google.com/recaptcha/docs/verify)."]]