Autenticazione nell'API Video Intelligence

Questa pagina descrive cos'è un account di servizio, come crearne uno per autenticare le richieste all'API Video Intelligence e come utilizzare il account di servizio per impostare le credenziali predefinite dell'applicazione.

Per consentire al codice dell'applicazione di utilizzare l'API Video Intelligence, devi configurare le credenziali appropriate per l'applicazione in modo che autentichi la propria identità nel servizio e ottenga l'autorizzazione per eseguire le attività. Questi meccanismi correlati alle credenziali sono noti come schemi di autenticazione.

L'autenticazione e l'autorizzazione API (comunemente raggruppate come "autenticazione") vengono in genere eseguite utilizzando un account di servizio account.Google Cloud Un account di servizio consente al tuo codice di inviare le credenziali dell'applicazione direttamente all'API Video Intelligence. Un service account, come un account utente, è rappresentato da un indirizzo email. A differenza di un account utente, un account di servizio è associato solo a un'applicazione.

Attivazione dell'API

Prima di poter eseguire l'autenticazione, devi abilitare l'API Video Intelligencee.

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Video Intelligence API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  6. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  7. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

  8. Install the Google Cloud CLI.

  9. Se utilizzi un provider di identità (IdP) esterno, devi prima accedere a gcloud CLI con la tua identità federata.

  10. Per inizializzare gcloud CLI, esegui questo comando:

    gcloud init
  11. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  12. Verify that billing is enabled for your Google Cloud project.

  13. Enable the Cloud Video Intelligence API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  14. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  15. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  16. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

  17. Install the Google Cloud CLI.

  18. Se utilizzi un provider di identità (IdP) esterno, devi prima accedere a gcloud CLI con la tua identità federata.

  19. Per inizializzare gcloud CLI, esegui questo comando:

    gcloud init
  20. Creazione di un account di servizio nella console Google Cloud

    Per creare un account di servizio utilizzando la console Google Cloud :

    1. Dalla pagina Credenziali della console Google Cloud , seleziona Crea credenziali > Chiave service account.

    2. Poi, nella sezione Service account, seleziona Nuovo service account.

    3. Nella casella Nome service account, inserisci un nome per il service account. Questo nome viene utilizzato come nome predefinito per l'ID service account (a sinistra di "@" nell'indirizzo ID service account generato), ma puoi modificarlo. Questi nomi sono arbitrari; l'importante è che tu li ricordi.

    4. In Tipo di chiave, seleziona JSON per la maggior parte dei nuovi progetti.

    5. Fai clic su Crea.

    La console Google Cloud genera quindi una chiave JSON (come file di testo .json), ti chiede di scaricare il file sul computer e visualizza una finestra di dialogo Account di servizio creato.

    La chiave JSON generata sarà simile alla seguente chiave JSON di esempio:

    {
      "type": "service_account",
      "project_id": "PROJECT_ID",
      "private_key_id": "SOME_NUMBER",
      "private_key": "-----BEGIN PRIVATE KEY-----\nPRIVATE_KEY\n-----END PRIVATE KEY-----\n",
      "client_email": "SERVICE_ACCOUNT_EMAIL",
      "client_id": "...",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://accounts.google.com/o/oauth2/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_EMAIL"
    }
    

    Archivia questo file JSON in modo sicuro, in quanto contiene la chiave privata (e questo file è l'unica copia della chiave). Devi fare riferimento a questo file delle chiavi per l'account di servizio all'interno del codice quando invii richieste di annotazione all'API Video Intelligence.

    Autenticazione con le Credenziali predefinite dell'applicazione

    Il modo più semplice per le applicazioni di autenticarsi all'API Video Intelligence è utilizzando le Credenziali predefinite dell'applicazione (ADC). I servizi che utilizzano ADC cercano prima le credenziali all'interno di una variabile di ambiente GOOGLE_APPLICATION_CREDENTIALS. A meno che tu non richieda specificamente che ADC utilizzi altre credenziali (ad esempio, le credenziali utente), devi impostare questa variabile di ambiente in modo che punti al file della chiave daccount di serviziount (il file .json scaricato quando hai creato una chiave account di servizioount.

    $ export GOOGLE_APPLICATION_CREDENTIALS=PATH_TO_SERVICE_ACCOUNT_FILE