Firebase ML Apple アプリの Cloud 認証情報を保護する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Apple アプリが Firebase ML の Cloud APIs のいずれかを使用している場合、本番環境でアプリをリリースする前に、不正な API アクセスを防ぐためにいくつかの追加手順を行う必要があります。
1. 既存の API キーの範囲を縮小する
まず、Cloud Vision API へのアクセスを許可しないように既存の API キーを構成します。
Google Cloud コンソールの [認証情報] ページを開きます。画面の指示に沿って、プロジェクトを選択します。
リスト内にある既存の API キーごとに、編集ビューを開きます。
[API の制限] セクションで [キーを制限] を選択し、API キーによるアクセスを許可するすべての API をリストに追加します。Cloud Vision API は含めないでください。
API キーの API の制限を構成することで、キーがアクセス権を持つ API を明示的に宣言します。デフォルトでは、[API の制限] セクションで [キーを制限しない] が選択されている場合、API キーを使用してプロジェクトで有効になっているすべての API にアクセスできます。
これで、既存の API キーはクラウド ML サービスへのアクセスを許可しなくなりますが、各キーは、API の制限リストに追加したすべての API で引き続き機能します。
今後 API を追加で有効にする場合は、該当する API キーの API の制限リストにそれらを追加する必要があります。
2. Firebase ML で使用する新しい API キーを作成する
次に、Cloud Vision API への呼び出しのみを許可する Firebase ML 用の新しい API キーを作成します。
認証情報ページに戻ります。Firebase プロジェクトが選択されていることを確認してください。
[認証情報を作成] > [API キー] の順にクリックします。新しい API キーをメモし、[キーを制限] をクリックします。
[API の制限] セクションで [キーを制限] を選択し、Cloud Vision API のみをリストに追加します。
この API キーは Cloud Vision API へのアクセスのみを許可し、Firebase ML がクラウドベースのモデルにアクセスするために使用できます。
3. 推奨: Cloud Vision API の割り当てを減らす
キーが不正使用された場合の影響を軽減するため、ユーザーごとの Cloud Vision API の割り当てをデフォルト設定より小さくすることをおすすめします。手順は次のとおりです。
Google Cloud Console の Cloud Vision API の割り当てページを開きます。画面の指示に沿って、プロジェクトを選択します。
[リクエスト] セクションで、[Requests per minute per user] の割り当てを、アプリに適した値に設定します。たとえば、アプリでドキュメントの画像をアップロードしてそのテキストを取得する場合、ユーザーがその操作を行うのは多くても数秒に 1 回と想定されるため、割り当ては 30~40 あれば十分であると考えられます。
ここで、「ユーザーごとのリクエスト数」は、単一の IP アドレスからのリクエストを指すことに注意してください。複数のユーザーが NAT を通じて同時にアプリを使用することが想定される場合は、この点を考慮する必要があります。
4. Firebase ML API キーを使用して Cloud APIs を呼び出す
最後に、アプリで新しい API キーを使用するように Firebase ML を構成します。
Firebase ML の API キーを使用すると Cloud Vision API への認証されていないアクセスが許可されるため、不正使用や請求先アカウントへの請求を防ぐために、キーを機密にしておくことが重要です。機密にするため、アプリのバイナリに API キーを含めないでください。代わりに、アプリのランタイム時に、既知の安全なユーザーがログインしていることを確認してから、サーバーから API キーを取得します。
これらの方法を守っても、API キーが侵害される可能性があります。上記のようにユーザーごとの API の割り当てを減らしたり、鍵のローテーション ポリシーを実装したり、異なるユーザー グループに異なるキーを発行したりするなど、キーが不正使用された場合の影響を軽減する対策を講じてください。
アプリで API キーを安全に取得したら、Firebase ML の Cloud API を呼び出すときにキーを指定します。
Swift
if let cloudVisionKey = getYourApiKey() { // See note above about securing your API key
let options = VisionCloudDetectorOptions()
options.apiKeyOverride = cloudVisionKey
let cloudDetector = Vision.vision().cloudLandmarkDetector(options: options)
}
Objective-C
NSString *cloudVisionKey = [self getYourApiKey]; // See note above about securing your API key
if (cloudVisionKey != nil) {
FIRVisionCloudDetectorOptions *options =
[[FIRVisionCloudDetectorOptions alloc] init];
options.APIKeyOverride = cloudVisionKey;
FIRVisionCloudLandmarkDetector *landmarkDetector =
[vision cloudLandmarkDetectorWithOptions:options];
}
また、API キーの保護の一般的なアドバイスに従ってください。
次のステップ
他の Firebase 機能を使用している場合のアプリのリリース準備方法については、リリース チェックリストをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-15 UTC。
[null,null,["最終更新日 2025-08-15 UTC。"],[],[],null,["\u003cbr /\u003e\n\nIf your Apple app uses one of Firebase ML's cloud APIs, before you launch your app\nin production, you should take some additional steps to prevent unauthorized\nAPI access.\n\n1. Reduce the scope of existing API keys\n\nFirst, 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\n2. Create a new API key for use with Firebase ML\n\nNext, create a new API key for Firebase ML that only allows calls to the\nCloud Vision API:\n\n1. Return to the [Credentials](https://console.cloud.google.com/apis/credentials) page. Be sure your\n Firebase project is still selected.\n\n2. Click **Create credentials \\\u003e API key** . Take note of the new API key, then\n click **Restrict key**.\n\n3. In the *API restrictions* section, select **Restrict key** , then add to the\n list ***only*** the Cloud Vision API.\n\nThis API key grants access only to the Cloud Vision API and can be used by\nFirebase ML to access cloud-based models.\n\n3. Recommended: Reduce the Cloud Vision API's quota\n\nTo help mitigate the consequences of a compromised key, you should reduce the\nCloud Vision API's per-user quota from its default setting. To do so:\n\n1. Open the [Cloud Vision API Quotas](https://console.cloud.google.com/apis/api/vision.googleapis.com/quotas?project=_) page of the\n Google Cloud console. When prompted, select your project.\n\n2. In the **Requests** section, set the **Requests per minute per user** quota\n to something reasonable for your app. For example, if your app involves\n uploading a picture of a document to get back its text, it's unlikely that a\n user will do that more than once every few seconds, so a quota of 30-40\n would probably be safe.\n\n Note that in this context \"requests per user\" refers to requests from a\n single IP address. You might need to consider this if you expect multiple\n users to use your app at the same time from behind a NAT.\n\n4. Call Cloud APIs using your Firebase ML API key\n\nFinally, in your app, configure Firebase ML to use your new API key.\n\nBecause the Firebase ML API key allows unauthenticated access to the Cloud Vision\nAPI, it's important to keep the key confidential to prevent unauthorized use and\ncharges to your billing account. To do so, you should refrain from including\nyour API key in your app binary. Instead, at app runtime, verify that a known\ngood user is signed in, and only then, retrieve the API key from a server.\n\nEven when these practices are observed, it is possible for an API key to be\ncompromised. You should take steps to help mitigate the consequences of a\ncompromised key, such as reducing the API's per-user quota as described above,\nimplementing key rotation policies, and issuing different keys to different\ngroups of users.\n\nAfter your app has safely acquired the API key, when you want to call an\nFirebase ML Cloud API, specify the key: \n\nSwift \n\n if let cloudVisionKey = getYourApiKey() { // See note above about securing your API key\n let options = VisionCloudDetectorOptions()\n options.apiKeyOverride = cloudVisionKey\n let cloudDetector = Vision.vision().cloudLandmarkDetector(options: options)\n }\n\nObjective-C \n\n NSString *cloudVisionKey = [self getYourApiKey]; // See note above about securing your API key\n if (cloudVisionKey != nil) {\n FIRVisionCloudDetectorOptions *options =\n [[FIRVisionCloudDetectorOptions alloc] init];\n options.APIKeyOverride = cloudVisionKey;\n FIRVisionCloudLandmarkDetector *landmarkDetector =\n [vision cloudLandmarkDetectorWithOptions:options];\n }\n\nIn addition, you should follow the general advice in\n[Securing an API key](https://cloud.google.com/docs/authentication/api-keys#securing_an_api_key).\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."]]