از اطلاعات کاربری Cloud برنامه Firebase ML Android خود محافظت کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
اگر برنامه Android شما از یکی از APIهای ابری Firebase ML استفاده می کند، قبل از اینکه برنامه خود را در مرحله تولید راه اندازی کنید، باید اقدامات دیگری را برای جلوگیری از دسترسی غیرمجاز API انجام دهید.
برای برنامه های تولیدی خود، مطمئن خواهید شد که فقط مشتریان تأیید شده می توانند به خدمات ابری دسترسی داشته باشند. (توجه داشته باشید که فقط دستگاههایی که روت نشدهاند میتوانند با استفاده از روش توضیح داده شده احراز هویت کنند.)
سپس، یک کلید API فقط اشکال زدایی ایجاد می کنید که می توانید برای راحتی در طول آزمایش و توسعه از آن استفاده کنید.
1. برنامه های تولیدی خود را با Firebase ثبت کنید
ابتدا برنامه های تولیدی خود را با Firebase ثبت کنید.
مطمئن شوید که امضاهای SHA-1 برنامه خود را دارید. برای یادگیری نحوه انجام آن، به احراز هویت مشتری خود مراجعه کنید.
به settingsپروژه خود در کنسول Firebase بروید، سپس تب تنظیمات را انتخاب کنید.
به سمت پایین به کارت برنامه های شما بروید، سپس برنامه Android خود را انتخاب کنید.
امضای SHA-1 برنامه خود را به اطلاعات برنامه خود اضافه کنید.
2. محدوده کلیدهای API خود را محدود کنید
سپس، کلیدهای API موجود خود را پیکربندی کنید تا دسترسی به Cloud Vision API ممنوع شود:
صفحه اعتبارنامه کنسول Google Cloud را باز کنید. وقتی از شما خواسته شد، پروژه خود را انتخاب کنید.
برای هر کلید API موجود در لیست، نمای ویرایش را باز کنید.
در بخش محدودیتهای API ، Restrict key را انتخاب کنید، سپس همه APIهایی را که میخواهید کلید API به آنها دسترسی داشته باشد، به لیست اضافه کنید. اطمینان حاصل کنید که Cloud Vision API را شامل نمی شود.
وقتی محدودیتهای API کلید API را پیکربندی میکنید، به صراحت APIهایی را که کلید به آنها دسترسی دارد، اعلام میکنید. بهطور پیشفرض، زمانی که در بخش محدودیتهای APIکلید Don't limited key انتخاب شده باشد، میتوان از یک کلید API برای دسترسی به هر API که برای پروژه فعال است استفاده کرد.
اکنون، کلیدهای API موجود شما امکان دسترسی به خدمات ابر ML را نمیدهند، اما هر کلید برای هر APIهایی که به لیست محدودیتهای API خود اضافه کردهاید به کار خود ادامه میدهد.
توجه داشته باشید که اگر در آینده هر API دیگری را فعال کنید، باید آنها را به لیست محدودیت های API برای کلید API قابل اجرا اضافه کنید.
3. یک کلید API فقط اشکال زدایی ایجاد کنید و از آن استفاده کنید
در نهایت، یک کلید API جدید ایجاد کنید تا فقط برای توسعه استفاده شود. Firebase ML میتواند از این کلید API برای دسترسی به خدمات Google Cloud در محیطهایی که احراز هویت برنامه امکانپذیر نیست، مانند زمانی که روی شبیهساز اجرا میشود، استفاده کند.
یک کلید API جدید ایجاد کنید تا برای توسعه استفاده شود:
صفحه اعتبارنامه کنسول Google Cloud را باز کنید. وقتی از شما خواسته شد، پروژه خود را انتخاب کنید.
روی Create credentials > API key کلیک کنید و کلید API جدید را یادداشت کنید. این کلید به API اجازه دسترسی از برنامههای احراز هویت نشده را میدهد، بنابراین این کلید را محرمانه نگه دارید .
برای اطمینان از اینکه کلید API اشکالزدایی جدید با برنامه منتشر شده شما فاش نمیشود، کلید API اشکالزدایی را در فایل مانیفست Android که فقط برای ساختهای اشکالزدایی استفاده میشود، مشخص کنید:
اگر قبلاً مانیفست اشکالزدایی ندارید، با کلیک روی File > New > Other > Android Manifest File و انتخاب debug از مجموعههای منبع هدف، یکی را ایجاد کنید.
در برنامه خود، Firebase ML را پیکربندی کنید تا از تطبیق اثر انگشت گواهی برای احراز هویت مشتری در تولید استفاده کند و از کلیدهای API - کلید اشکالزدایی - فقط در ساختهای اشکالزدایی استفاده کند:
Kotlin
valoptionsBuilder=FirebaseVisionCloudImageLabelerOptions.Builder()if(!BuildConfig.DEBUG){// Requires physical, non-rooted device:optionsBuilder.enforceCertFingerprintMatch()}// Set other options. For example:optionsBuilder.setConfidenceThreshold(0.8f)// ...// And lastly:valoptions=optionsBuilder.build()FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage)
FirebaseVisionCloudImageLabelerOptions.BuilderoptionsBuilder=newFirebaseVisionCloudImageLabelerOptions.Builder();if(!BuildConfig.DEBUG){// Requires physical, non-rooted device:optionsBuilder.enforceCertFingerprintMatch();}// Set other options. For example:optionsBuilder.setConfidenceThreshold(0.8f);// ...// And lastly:FirebaseVisionCloudImageLabelerOptionsoptions=optionsBuilder.build();FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage);
تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی."],[],[],null,["\u003cbr /\u003e\n\nIf your Android app uses one of Firebase ML's cloud APIs, before you launch your\napp in production, you should take some additional steps to prevent\nunauthorized API access.\n\nFor your production apps, you will ensure that only authenticated clients can\naccess cloud services. (Note that only non-rooted devices can authenticate using\nthe method described.)\n\nThen, you will create a debug-only API key that you can use for convenience\nduring testing and development.\n\n1. Register your production apps with Firebase\n\nFirst, register your production apps with Firebase.\n\n1. Make sure that you have your app's SHA-1 signatures. Refer to\n [Authenticating your client](//developers.google.com/android/guides/client-auth)\n to learn how.\n\n2. Go to your settings\n *Project settings* in the Firebase console, then select the *Settings*\n tab.\n\n3. Scroll down to the *Your apps* card, then select your Android app.\n\n4. Add your app's SHA-1 signature to your app's information.\n\n2. Restrict the scope of your API keys\n\nNext, configure your existing API keys to disallow access to the Cloud Vision\nAPI:\n\n1. Open the [Credentials](https://console.cloud.google.com/apis/credentials?project=_) page of the\n Google Cloud console. When prompted, select your project.\n\n2. For each existing API key in the list, open the editing view.\n\n3. In the *API restrictions* section, select **Restrict key** , then add to the\n list all of the APIs to which you want the API key to have access. Make sure\n to ***not*** include the Cloud Vision API.\n\n When you configure an API key's *API restrictions* , you are explicitly\n declaring the APIs to which the key has access. **By default, when the *API\n restrictions* section has *Don't restrict key* selected, an API key can be\n used to access any API that is enabled for the project.**\n\nNow, your existing API keys will not grant access to cloud ML services, but each\nkey will continue to work for any APIs that you added to its *API restrictions*\nlist.\n\nNote that if you enable any additional APIs in the future, you must add them to\nthe *API restrictions* list for the applicable API key.\n\n3. Create and use a debug-only API key\n\nFinally, create a new API key to be used only for development. Firebase ML can\nuse this API key to access Google Cloud services in environments where app\nauthentication isn't possible, such as when running on emulators.\n\n1. Create a new API key to be used for development:\n\n 1. Open the [Credentials](https://console.cloud.google.com/apis/credentials?project=_) page of the\n Google Cloud console. When prompted, select your project.\n\n 2. Click **Create credentials \\\u003e API key** and take note of the new API\n key. This key allows API access from unauthenticated apps, so\n **keep this key confidential**.\n\n2. To ensure the new debug API key is not leaked with your released app,\n specify the debug API key in an Android manifest file used only for debug\n builds:\n\n 1. If you don't already have a debug manifest, create one by clicking\n **File \\\u003e New \\\u003e Other \\\u003e Android Manifest File** and selecting `debug`\n from the target source sets.\n\n 2. In the debug manifest, add the following declaration:\n\n ```text\n \u003capplication\u003e\n \u003cmeta-data\n android:name=\"com.firebase.ml.cloud.ApiKeyForDebug\"\n android:value=\"your-debug-api-key\" /\u003e\n \u003c/application\u003e\n ```\n3. In your app, configure Firebase ML to use certificate fingerprint matching to\n authenticate your client in production and to use API keys---the debug\n key---only in debug builds:\n\n Kotlin \n\n ```kotlin\n val optionsBuilder = FirebaseVisionCloudImageLabelerOptions.Builder()\n if (!BuildConfig.DEBUG) {\n // Requires physical, non-rooted device:\n optionsBuilder.enforceCertFingerprintMatch()\n }\n\n // Set other options. For example:\n optionsBuilder.setConfidenceThreshold(0.8f)\n // ...\n\n // And lastly:\n val options = optionsBuilder.build()\n FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage)https://github.com/firebase/snippets-android/blob/b694d4dbd411d31be39655f47691c3e9f3529b03/mlkit/app/src/main/java/com/google/firebase/example/mlkit/kotlin/MainActivity.kt#L30-L42\n ```\n\n Java \n\n ```java\n FirebaseVisionCloudImageLabelerOptions.Builder optionsBuilder =\n new FirebaseVisionCloudImageLabelerOptions.Builder();\n if (!BuildConfig.DEBUG) {\n // Requires physical, non-rooted device:\n optionsBuilder.enforceCertFingerprintMatch();\n }\n\n // Set other options. For example:\n optionsBuilder.setConfidenceThreshold(0.8f);\n // ...\n\n // And lastly:\n FirebaseVisionCloudImageLabelerOptions options = optionsBuilder.build();\n FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage);https://github.com/firebase/snippets-android/blob/b694d4dbd411d31be39655f47691c3e9f3529b03/mlkit/app/src/main/java/com/google/firebase/example/mlkit/MainActivity.java#L30-L43\n ```\n\nNext steps\n\nSee the [launch checklist](/support/guides/launch-checklist) for information on\npreparing your app to launch when using other Firebase features."]]