In questa pagina viene descritto come creare, modificare e limitare le chiavi API.
Introduzione alle chiavi API
Quando utilizzi una chiave API per l'autenticazione in un'API, la chiave API non identifica un'entità. La chiave API associa una richiesta con un progetto Google Cloud ai fini della fatturazione e della quota. Senza un entità, la richiesta non può utilizzare Identity and Access Management (IAM) per verificare se chi chiama è autorizzato a eseguire l'operazione richiesta.
Una chiave API è composta dai seguenti componenti, che utilizzi per gestirla e utilizzarla:
- Stringa
- La stringa della chiave API è una stringa criptata, ad esempio
AIzaSyDaGmWKa4JsXZ-HjGw7ISLn_3namBGewQe
. Quando utilizzi una chiave API per autentica, utilizzerai sempre la stringa della chiave. Le chiavi API non hanno file JSON associato. - ID
- L'ID chiave API viene utilizzato dagli strumenti di amministrazione di Google Cloud per identificare in modo univoco la chiave. L'ID chiave non può essere utilizzato per l'autenticazione. L'ID chiave può essere presente nell'URL della pagina di modifica della chiave nella console Google Cloud. Puoi anche recuperare l'ID chiave utilizzando Google Cloud CLI per elencare le chiavi nel tuo progetto.
- Nome visualizzato
- Il nome visualizzato è un nome descrittivo facoltativo della chiave, che puoi impostare quando crei o aggiorni la chiave.
Per gestire le chiavi API devi disporre del ruolo Amministratore chiavi API
(roles/serviceusage.apiKeysAdmin
) sul progetto.
Prima di iniziare
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
C++
Per utilizzare gli C++ esempi in questa pagina in una località dell'ambiente di sviluppo, installare e inizializzare gcloud CLI quindi configura Credenziali predefinite dell'applicazione con le tue credenziali utente.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
Per ulteriori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale nella documentazione sull'autenticazione di Google Cloud.
Java
Per utilizzare gli Java esempi in questa pagina in un ambiente di sviluppo locale, installa e inizializza l'interfaccia a riga di comando gcloud, quindi configura le credenziali predefinite dell'applicazione con le tue credenziali utente.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
Per ulteriori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale nella documentazione sull'autenticazione di Google Cloud.
Python
Per utilizzare gli Python esempi in questa pagina in una località dell'ambiente di sviluppo, installare e inizializzare gcloud CLI quindi configura Credenziali predefinite dell'applicazione con le tue credenziali utente.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
Per ulteriori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale nella documentazione sull'autenticazione di Google Cloud.
REST
Per utilizzare gli esempi dell'API REST in questa pagina in un ambiente di sviluppo locale, utilizza le credenziali fornite a gcloud CLI.
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
Per ulteriori informazioni, vedi Esegui l'autenticazione per l'utilizzo di REST nella documentazione sull'autenticazione di Google Cloud.
crea una chiave API
Per creare una chiave API, utilizza una delle seguenti opzioni:
Console
Nella console Google Cloud, vai alla pagina Credenziali:
Fai clic su Crea credenziali, quindi seleziona Chiave API dal menu.
La finestra di dialogo Chiave API creata mostra la stringa per i file chiave creata.
gcloud
Puoi utilizzare
Comando gcloud services api-keys create
per creare una chiave API.
Sostituisci DISPLAY_NAME
con un nome descrittivo per la chiave.
gcloud services api-keys create --display-name=DISPLAY_NAME
C++
Per eseguire questo esempio, devi installare Libreria client delle chiavi API.
Java
Per eseguire questo esempio, devi installare
Libreria client di google-cloud-apikeys
.
Python
Per eseguire questo esempio, devi installare la libreria client API Keys.
REST
Utilizza il
metodo keys.create
per creare una chiave API. Questa richiesta restituisce un'operazione di lunga durata; devi eseguire il polling dell'operazione per ottenere le informazioni sulla nuova chiave.
Sostituisci i seguenti valori:
DISPLAY_NAME
: facoltativo. Un nome descrittivo per la chiave.PROJECT_ID
: l'ID o il nome del tuo progetto Google Cloud.
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"
Per ulteriori informazioni sulla creazione di chiavi API mediante l'API REST, consulta Creazione di una chiave API nel documentazione dell'API API Keys.
Copia la stringa della chiave e tienila al sicuro. Utilizza le restrizioni relative alle chiavi API per limitare l'utilizzo della chiave.
Applica limitazioni relative alle chiavi API
Le chiavi API non sono soggette a restrizioni per impostazione predefinita. Le chiavi senza restrizioni non sono sicure perché possono essere utilizzati da chiunque, ovunque. Per le applicazioni di produzione, devi impostare sia le limitazioni delle applicazioni sia le limitazioni delle API.
Aggiungere limitazioni delle applicazioni
Le limitazioni delle applicazioni specificano quali siti web, indirizzi IP o app possono utilizzare una chiave API.
Puoi applicare un solo tipo di limitazione delle applicazioni alla volta. Scegli il tipo di restrizione in base al tipo di applicazione:
Opzione | Tipo di applicazione | Note |
---|---|---|
Referrer HTTP | Applicazioni web | Specifica i siti web che possono utilizzare la chiave. |
Indirizzi IP | Applicazioni richiamate da server specifici | Specifica i server o i cron job che possono utilizzare la chiave. |
App per Android | App Android | Specifica l'applicazione Android che può utilizzare la chiave. |
App per iOS | Applicazioni iOS | Specifica i bundle per iOS che possono utilizzare la chiave. |
Referrer HTTP
Per limitare i siti web che possono utilizzare la tua chiave API, aggiungi una o più limitazioni dei referrer HTTP.
Puoi sostituire un carattere jolly (*
) per il sottodominio o il percorso, ma
non puoi inserire un carattere jolly nel mezzo dell'URL. Ad esempio,*.example.com
è valido e accetta tutti i siti che terminano con .example.com
. Tuttavia,
mysubdomain*.example.com
non è una restrizione valida.
I numeri di porta possono essere inclusi nelle restrizioni dei referrer HTTP. Se includi un numero di porta, verranno associate solo le richieste che utilizzano quella porta. In caso contrario se specifichi un numero di porta, verranno soddisfatte le richieste provenienti da qualsiasi numero di porta.
Puoi aggiungere fino a 1200 referrer HTTP a una chiave API.
La tabella seguente mostra alcuni scenari di esempio e limitazioni del browser:
Scenario | Restrizioni |
---|---|
Consentire un URL specifico | Aggiungi un URL con un percorso esatto. Ad esempio:www.example.com/path www.example.com/path/path Alcuni browser implementano un tag criterio referrer che invia solo l'URL di origine per le richieste multiorigine. Gli utenti di questi browser non possono utilizzare chiavi con restrizioni URL specifiche della pagina. |
Consentire qualsiasi URL nel sito | Devi impostare due URL nell'elenco allowedReferers .
|
Consenti qualsiasi URL in un singolo sottodominio o dominio semplice |
Devi impostare due URL nell'elenco
|
Per limitare la chiave API a siti web specifici, utilizza una delle seguenti opzioni:
Console
Nella console Google Cloud, vai alla pagina Credenziali:
Fai clic sul nome della chiave API che vuoi limitare.
Nella sezione Restrizioni delle applicazioni, seleziona Referrer HTTP.
Per ogni restrizione che vuoi aggiungere, fai clic su Aggiungi un elemento, inserisci la restrizione e fai clic su Fine.
Fai clic su Salva per salvare le modifiche e tornare all'elenco delle chiavi API.
gcloud
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi recuperare l'ID utilizzando il comando
gcloud services api-keys list
per elencare le chiavi del progetto.Utilizza il comando
gcloud services api-keys update
per aggiungere limitazioni dei referrer HTTP a una chiave API.Sostituisci i seguenti valori:
KEY_ID
: l'ID della chiave che vuoi limitare.ALLOWED_REFERRER_1
: il tuo referrer HTTP delle risorse.Puoi aggiungere tutte le restrizioni che vuoi. utilizza le virgole per separare le restrizioni. Devi fornire tutte le restrizioni per i referrer con il parametro il comando update; le limitazioni relative ai referrer fornite sostituiscono eventuali restrizioni del referrer sulla chiave.
gcloud services api-keys update KEY_ID \ --allowed-referrers="ALLOWED_REFERRER_1"
Java
Per eseguire questo esempio, devi installare
Libreria client di google-cloud-apikeys
.
Python
Per eseguire questo esempio, devi installare Libreria client delle chiavi API.
REST
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi ottenere l'ID utilizzando il metodo keys.list. L'ID è indicato nel campo
uid
della risposta.Sostituisci
PROJECT_ID
con il tuo progetto Google Cloud ID o nome.curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/"
Utilizza il metodo keys.patch per aggiungere limitazioni dei referrer HTTP alla chiave API.
Questa richiesta restituisce un'operazione a lunga esecuzione; devi eseguire il polling dell'operazione quando l'operazione viene completata e conoscerne lo stato.
Sostituisci i seguenti valori:
ALLOWED_REFERRER_1
: la tua limitazione del referrer HTTP.Puoi aggiungere tutte le limitazioni necessarie. Utilizza le virgole per separare le limitazioni. Devi fornire tutte le limitazioni dei referrer con la richiesta. Le limitazioni dei referrer fornite sostituiscono eventuali limitazioni dei referrer esistenti nella chiave.
PROJECT_ID
: l'ID del tuo progetto Google Cloud o nome.KEY_ID
: l'ID della chiave che vuoi limitare.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ --data '{ "restrictions" : { "browserKeyRestrictions": { "allowedReferrers": ["ALLOWED_REFERRER_1"] } } }' \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/KEY_ID?updateMask=restrictions"
Per ulteriori informazioni sull'aggiunta di limitazioni dei referrer HTTP a una chiave utilizzando l'API REST, consulta Aggiunta di limitazioni del browser nella documentazione dell'API Chiave API.
Indirizzi IP
Puoi specificare uno o più indirizzi IP dei chiamanti, ad esempio un web server o un cron job, autorizzati a utilizzare la tua chiave API. Puoi specificare indirizzi IP in uno dei seguenti formati:
- IPv4 (
198.51.100.1
) - IPv6 (
2001:db8::1
) - Una subnet che utilizza la notazione CIDR (
198.51.100.0/24
,2001:db8::/64
)
L'utilizzo di localhost
non è supportato per le limitazioni del server.
Per limitare la chiave API a indirizzi IP specifici, utilizza una delle seguenti opzioni: opzioni:
Console
Nella console Google Cloud, vai alla pagina Credenziali:
Fai clic sul nome della chiave API che vuoi limitare.
Nella sezione Restrizioni delle applicazioni, seleziona Indirizzi IP.
Per ogni indirizzo IP che vuoi aggiungere, fai clic su Aggiungi un elemento, inserisci l'indirizzo e fai clic su Fine.
Fai clic su Salva per salvare le modifiche e tornare all'elenco delle chiavi API.
gcloud
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi ottenere l'ID utilizzando Comando
gcloud services api-keys list
per elencare le chiavi nel tuo progetto.Utilizza la Comando
gcloud services api-keys update
per aggiungere restrizioni del server (indirizzo IP) a una chiave API.Sostituisci i seguenti valori:
KEY_ID
: l'ID della chiave che vuoi limitare.ALLOWED_IP_ADDR_1
: il tuo indirizzo IP consentito.Puoi aggiungere tutti gli indirizzi IP necessari; utilizza le virgole per separare gli indirizzi.
gcloud services api-keys update KEY_ID \ --allowed-ips="ALLOWED_IP_ADDR_1"
Java
Per eseguire questo esempio, devi installare la
libreria client google-cloud-apikeys
.
Python
Per eseguire questo esempio, devi installare Libreria client delle chiavi API.
REST
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi ottenere l'ID utilizzando keys.list . L'ID è elencato nel campo
uid
della risposta.Sostituisci
PROJECT_ID
con l'ID o il nome del progetto Google Cloud.curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/"
Utilizza il metodo keys.patch per aggiungere limitazioni del server (indirizzo IP) a una chiave API.
Questa richiesta restituisce un'operazione a lunga esecuzione; devi eseguire il polling dell'operazione quando l'operazione viene completata e conoscerne lo stato.
Sostituisci i seguenti valori:
ALLOWED_IP_ADDR_1
: il tuo indirizzo IP consentito.Puoi aggiungere tutti gli indirizzi IP necessari; utilizza le virgole per separare le limitazioni. Devi fornire tutti gli indirizzi IP con il richiesta; le limitazioni relative ai referrer fornite sostituiscono eventuali Limitazioni dell'indirizzo IP della chiave.
PROJECT_ID
: il nome o l'ID del tuo progetto Google Cloud.KEY_ID
: l'ID della chiave che vuoi limitare.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ --data '{ "restrictions" : { "serverKeyRestrictions": { "allowedIps": ["ALLOWED_IP_ADDR_1"] } } }' \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/KEY_ID?updateMask=restrictions"
Per saperne di più sull'aggiunta di limitazioni degli indirizzi IP a una chiave utilizzando l'API REST, consulta Aggiunta di limitazioni del server nella documentazione dell'API Chiave API.
App Android
Puoi limitare l'utilizzo di una chiave API a specifiche app per Android. Devi fornire il nome del pacchetto e la fingerprint del certificato SHA-1 di 20 byte per ogni app.
Quando utilizzi la chiave API in una richiesta, devi specificare il nome del pacchetto e la fingerprint del certificato utilizzando le seguenti intestazioni HTTP:
X-Android-Package
X-Android-Cert
Per limitare la chiave API a una o più app per Android, utilizza una delle seguenti opzioni: opzioni:
Console
Nella console Google Cloud, vai alla pagina Credenziali:
Fai clic sul nome della chiave API che vuoi limitare.
Nella sezione Restrizioni delle applicazioni, seleziona App per Android.
Per ogni app per Android che vuoi aggiungere, fai clic su Aggiungi un elemento e inserisci il nome del pacchetto e la fingerprint del certificato SHA-1, quindi fai clic su Fine.
Fai clic su Salva per salvare le modifiche e tornare all'elenco delle chiavi API.
gcloud
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi recuperare l'ID utilizzando il comando
gcloud services api-keys list
per elencare le chiavi del progetto.Utilizza la Comando
gcloud services api-keys update
per specificare le app per Android che possono utilizzare una chiave API.Sostituisci i seguenti valori:
KEY_ID
: l'ID della chiave che vuoi limitare.SHA1_FINGERPRINT
ePACKAGE_NAME
: le informazioni sull'app per un'app Android che può utilizzare la chiave.Puoi aggiungere tutte le app che vuoi. utilizza ulteriori
--allowed-application
flag.
gcloud services api-keys update KEY_ID \ --allowed-application=sha1_fingerprint=SHA1_FINGERPRINT_1,package_name=PACKAGE_NAME_1 \ --allowed-application=sha1_fingerprint=SHA1_FINGERPRINT_2,package_name=PACKAGE_NAME_2
Java
Per eseguire questo esempio, devi installare
Libreria client di google-cloud-apikeys
.
Python
Per eseguire questo esempio, devi installare la libreria client API Keys.
REST
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi ottenere l'ID utilizzando il metodo keys.list. L'ID è elencato nel campo
uid
della risposta.Sostituisci
PROJECT_ID
con il tuo progetto Google Cloud ID o nome.curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/"
Utilizza la keys.patch per specificare le app Android che possono utilizzare una chiave API.
Questa richiesta restituisce un'operazione a lunga esecuzione. Devi eseguire il polling dell'operazione per sapere quando viene completata e ottenere lo stato dell'operazione.
Sostituisci i seguenti valori:
SHA1_FINGERPRINT_1
e PACKAGE_NAME_1: le informazioni sull'app per un'app Android che può utilizzare la chiave.Puoi aggiungere le informazioni per tutte le app necessarie. Utilizza le virgole per separare gli oggetti AndroidApplication. Devi fornire tutte le applicazioni con la richiesta. Le applicazioni fornite sostituiscono eventuali applicazioni consentite esistenti nella chiave.
PROJECT_ID
: l'ID del tuo progetto Google Cloud o nome.KEY_ID
: l'ID della chiave che vuoi limitare.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ --data '{ "restrictions" : { "androidKeyRestrictions": { "allowedApplications": [ { "sha1Fingerprint": "SHA1_FINGERPRINT_1", "packageName": "PACKAGE_NAME_1" }, ] } } }' \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/KEY_ID?updateMask=restrictions"
Per ulteriori informazioni sull'aggiunta di limitazioni delle app per Android a una chiave tramite il API REST, consulta Aggiunta di limitazioni Android nella documentazione dell'API API Keys.
App per iOS
Puoi limitare l'utilizzo di una chiave API ad app per iOS specifiche fornendo l'ID pacchetto di ciascuna app.
Quando utilizzi la chiave API in una richiesta, devi specificare l'ID pacchetto utilizzando
l'intestazione HTTP X-Ios-Bundle-Identifier
.
Per limitare la chiave API a una o più app per iOS, utilizza una delle seguenti opzioni:
Console
Nella console Google Cloud, vai alla pagina Credenziali:
Fai clic sul nome della chiave API che vuoi limitare.
Nella sezione Restrizioni delle applicazioni, seleziona App per iOS.
Per ogni app per iOS che vuoi aggiungere, fai clic su Aggiungi un elemento e inserisci l'ID pacchetto, quindi fai clic su Fine.
Fai clic su Salva per salvare le modifiche e tornare all'elenco delle chiavi API.
gcloud
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi ottenere l'ID utilizzando Comando
gcloud services api-keys list
per elencare le chiavi nel tuo progetto.Utilizza la
gcloud services api-keys update
per specificare le app per iOS che possono utilizzare la chiave.Sostituisci i seguenti valori:
KEY_ID
: l'ID della chiave che vuoi limitare.ALLOWED_BUNDLE_ID
: l'ID bundle di un'app per iOS per la quale vuoi poter utilizzare questa chiave API.Puoi aggiungere tutti gli ID set di pacchetti necessari. Utilizza le virgole per separare gli ID.
gcloud services api-keys update KEY_ID \ --allowed-bundle-ids=ALLOWED_BUNDLE_ID_1,ALLOWED_BUNDLE_ID_2
Java
Per eseguire questo esempio, devi installare la
libreria client google-cloud-apikeys
.
Python
Per eseguire questo esempio, devi installare Libreria client delle chiavi API.
REST
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi ottenere l'ID utilizzando il metodo keys.list. L'ID è indicato nel campo
uid
della risposta.Sostituisci
PROJECT_ID
con il tuo progetto Google Cloud ID o nome.curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/"
Utilizza il metodo keys.patch per specificare le app per iOS che possono utilizzare una chiave API.
Questa richiesta restituisce un'operazione a lunga esecuzione. Devi eseguire il polling dell'operazione per sapere quando viene completata e ottenere lo stato dell'operazione.
Sostituisci i seguenti valori:
ALLOWED_BUNDLE_ID
: l'ID pacchetto di un'app per iOS che possono utilizzare la chiave.Puoi aggiungere le informazioni per tutte le app necessarie. Utilizza le virgole per separare gli ID bundle. Devi fornire tutti gli ID bundle con la richiesta. Gli ID bundle forniti sostituiscono eventuali applicazioni consentite esistenti nella chiave.
PROJECT_ID
: il nome o l'ID del tuo progetto Google Cloud.KEY_ID
: l'ID della chiave che vuoi limitare.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ --data '{ "restrictions" : { "iosKeyRestrictions": { "allowedBundleIds": ["ALLOWED_BUNDLE_ID_1","ALLOWED_BUNDLE_ID_2"] } } }' \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/KEY_ID?updateMask=restrictions"
Per ulteriori informazioni sull'aggiunta di limitazioni per le app per iOS a una chiave tramite REST sull'API, consulta Aggiunta di limitazioni iOS nella documentazione dell'API API Keys.
Aggiungere limitazioni dell'API
Le restrizioni delle API specificano quali API possono essere chiamate utilizzando la chiave API.
Per aggiungere restrizioni alle API, utilizza una delle seguenti opzioni:
Console
Nella console Google Cloud, vai alla pagina Credenziali:
Fai clic sul nome della chiave API che vuoi limitare.
Nella sezione Restrizioni API, fai clic su Limita chiave.
Seleziona tutte le API alle quali accederà la tua chiave API.
Fai clic su Salva per salvare le modifiche e tornare all'elenco delle chiavi API.
gcloud
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi recuperare l'ID utilizzando il comando
gcloud services api-keys list
per elencare le chiavi del progetto.Utilizza il comando
gcloud services api-keys update
per specificare i servizi per i quali è possibile utilizzare una chiave API per l'autenticazione.Sostituisci i seguenti valori:
KEY_ID
: l'ID della chiave che vuoi limitare.SERVICE_1
,SERVICE_2
…: i nomi dei servizi delle API a cui è possibile accedere con la chiave.Devi fornire tutti i nomi dei servizi con il comando di aggiornamento. I nomi dei servizi forniti sostituiscono eventuali servizi esistenti nella chiave.
Puoi trovare il nome del servizio cercando l'API nella dashboard delle API. I nomi del servizio sono stringhe come
bigquery.googleapis.com
.gcloud services api-keys update KEY_ID \ --api-target=service=SERVICE_1 --api-target=service=SERVICE_2
Java
Per eseguire questo esempio, devi installare
Libreria client di google-cloud-apikeys
.
Python
Per eseguire questo esempio, devi installare la libreria client API Keys.
REST
Recupera l'ID della chiave che vuoi limitare.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi ottenere l'ID utilizzando keys.list . L'ID è elencato nel campo
uid
della risposta.Sostituisci
PROJECT_ID
con il tuo progetto Google Cloud ID o nome.curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/"
Utilizza il metodo keys.patch per specificare per quali servizi è possibile utilizzare una chiave API per l'autenticazione.
Questa richiesta restituisce un'operazione a lunga esecuzione. Devi eseguire il polling dell'operazione per sapere quando viene completata e ottenere lo stato dell'operazione.
Sostituisci i seguenti valori:
SERVICE_1
,SERVICE_2
...: I nomi dei servizi delle API a cui è possibile utilizzare la chiave per accedere.Devi fornire tutti i nomi dei servizi nella richiesta. il servizio i nomi forniti sostituiscono eventuali servizi esistenti nella chiave.
Puoi trovare il nome del servizio cercando l'API nella dashboard delle API. Servizio i nomi sono stringhe come
bigquery.googleapis.com
.PROJECT_ID
: il nome o l'ID del tuo progetto Google Cloud.KEY_ID
: l'ID della chiave che vuoi limitare.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ --data '{ "restrictions" : { "apiTargets": [ { "service": "SERVICE_1" }, { "service" : "SERVICE_2" }, ] } }' \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/KEY_ID?updateMask=restrictions"
Per ulteriori informazioni sull'aggiunta di restrizioni API a una chiave utilizzando l'API REST, consulta Aggiunta di restrizioni API nella documentazione dell'API Chiave API.
Recupera le informazioni sul progetto da una stringa chiave
Puoi determinare a quale progetto Google Cloud è associata una chiave API dalla relativa stringa.
Sostituisci KEY_STRING
con la stringa chiave per cui hai bisogno di informazioni sul progetto.
gcloud
Puoi utilizzare
Comando gcloud services api-keys lookup
per ottenere l'ID progetto da una stringa chiave.
gcloud services api-keys lookup KEY_STRING
Java
Per eseguire questo esempio, devi installare
Libreria client di google-cloud-apikeys
.
Python
Per eseguire questo esempio, devi installare Libreria client delle chiavi API.
REST
Utilizza il
metodo lookupKey
per ottenere l'ID progetto da una stringa chiave.
curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ "https://apikeys.googleapis.com/v2/keys:lookupKey?keyString=KEY_STRING"
Annullamento eliminazione di una chiave API
Se elimini una chiave API per errore, puoi annullarne l'eliminazione (ripristinarla) all'interno 30 giorni dall'eliminazione della chiave. Dopo 30 giorni, non potrai annullare l'eliminazione della chiave API.
Console
Nella console Google Cloud, vai alla pagina Credenziali:
Fai clic su Ripristina credenziali eliminate.
Trova la chiave API eliminata di cui vuoi annullare l'eliminazione e fai clic su Ripristina.
L'annullamento dell'eliminazione di una chiave API potrebbe richiedere alcuni minuti per propagarsi. Dopo la propagazione, la chiave API non eliminata viene visualizzata nell'elenco delle chiavi API.
gcloud
Recupera l'ID della chiave di cui vuoi annullare l'eliminazione.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi ottenere l'ID utilizzando Comando
gcloud services api-keys list --show-deleted
per elencare le chiavi eliminate nel progetto.Utilizza il comando
gcloud services api-keys undelete
per annullare l'eliminazione di una chiave API.gcloud services api-keys undelete KEY_ID
Sostituisci i seguenti valori:
KEY_ID
: l'ID della chiave di cui vuoi annullare l'eliminazione.
REST
Recupera l'ID della chiave di cui vuoi annullare l'eliminazione.
L'ID non corrisponde al nome visualizzato o alla stringa della chiave. Puoi recuperare l'ID utilizzando il metodo keys.list con il parametro di query
showDeleted
impostato sutrue
. L'ID chiave è elencato nel campouid
della risposta.Sostituisci
PROJECT_ID
con il tuo progetto Google Cloud ID o nome.curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys?showDeleted=true"
Utilizza la annullare eliminazione per annullare l'eliminazione della chiave API.
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ "https://apikeys.googleapis.com/v2/projects/PROJECT_ID/locations/global/keys/KEY_ID:undelete"
Questa richiesta restituisce un'operazione a lunga esecuzione; devi eseguire il polling dell'operazione quando l'operazione viene completata e conoscerne lo stato.
Sostituisci i seguenti valori:
PROJECT_ID
: il nome o l'ID del tuo progetto Google Cloud.KEY_ID
: l'ID della chiave che vuoi limitare.
Esegui il polling delle operazioni a lunga esecuzione
I metodi API Chiavi API utilizzano operazioni a lunga esecuzione. Se utilizzi l'API REST per creare e gestire le chiavi API, dalla richiesta iniziale del metodo viene restituito un oggetto Operation. Utilizza il nome dell'operazione per eseguire il polling dell'operazione a lunga esecuzione. Al completamento della richiesta a lunga esecuzione, il polling dell'operazione restituisce il valore i dati della richiesta a lunga esecuzione.
Per eseguire il polling di un'operazione dell'API Chiave API a lunga esecuzione, utilizza il metodo
operations.get
.
Sostituisci OPERATION_NAME
con il nome dell'operazione restituito
dall'operazione a lunga esecuzione. Ad esempio:
operations/akmf.p7-358517206116-cd10a88a-7740-4403-a8fd-979f3bd7fe1c
.
curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ "https://apikeys.googleapis.com/v2/OPERATION_NAME"
Limiti per le chiavi API
Puoi creare fino a 300 chiavi API per progetto. Questo limite è un limite di sistema e non può essere modificato utilizzando una richiesta di aumento della quota.
Se sono necessarie più chiavi API, devi utilizzare più di un progetto.
Passaggi successivi
- Scopri le best practice per proteggere le chiavi API.
- Scopri di più sull'API API Keys.
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2024-10-10 UTC.