Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cette page explique comment authentifier les appels aux services Vertex AI sur Google Distributed Cloud (GDC) air-gapped. Vous devez configurer l'authentification par jeton pour sécuriser vos requêtes adressées à l'API Vertex AI dans vos applications isolées. Ce processus valide vos requêtes API en fournissant votre identité et en autorisant vos interactions.
Cette page s'adresse aux développeurs d'applications des groupes d'opérateurs d'applications chargés de configurer leurs environnements d'application et de développement pour activer les fonctionnalités d'IA. Pour en savoir plus, consultez la documentation sur les audiences pour GDC en mode air-gapped.
Veillez à mettre à jour votre magasin de confiance local avant de configurer l'authentification dans votre environnement de développement.
S'authentifier auprès des services Vertex AI
Les interactions avec les services Vertex AI se font à l'aide de jetons d'authentification. Les jetons sont des objets numériques qui valident votre identité et votre autorisation une fois que vous avez fourni des identifiants valides. Le jeton contient des informations spécifiques sur votre compte et les autorisations dont il dispose pour accéder aux services et ressources et les utiliser.
Il existe deux façons de configurer l'authentification :
Accédez au service Vertex AI ou au modèle d'IA générative que vous souhaitez utiliser en attribuant à votre compte utilisateur le rôle correspondant listé dans Préparer les autorisations IAM.
Connectez-vous à Distributed Cloud avec le compte utilisateur que vous devez utiliser pour interagir avec l'API :
Selon l'utilisation prévue du jeton d'authentification, vous devrez peut-être inclure le port après le point de terminaison du service dans le chemin d'accès aux audiences, comme suit :
Si vous utilisez une bibliothèque cliente pour votre requête, vous devez inclure le port :443 après le point de terminaison du service dans le chemin d'accès aux audiences. Par conséquent, le chemin --audiences dans la commande doit être https://ENDPOINT:443.
Si vous utilisez gRPC, curl ou des appels REST programmatiques pour votre demande, n'incluez pas le port. Par conséquent, le chemin --audiences dans la commande doit être https://ENDPOINT.
Le résultat affiche le jeton d'authentification. Ajoutez le jeton à l'en-tête des requêtes de ligne de commande que vous effectuez, comme dans l'exemple suivant :
-H"Authorization: Bearer TOKEN"
Remplacez TOKEN par la valeur du jeton d'authentification affiché dans la sortie.
S'authentifier avec votre compte de service
Les instructions suivantes vous expliquent comment obtenir un jeton d'authentification pour votre compte de service :
Attribuez au compte de service le rôle correspondant indiqué dans Préparer les autorisations IAM pour lui permettre d'accéder au service ou au modèle que vous souhaitez utiliser.
Remplacez PATH_TO_SERVICE_KEY par le chemin d'accès au fichier JSON contenant les paires de clés de votre compte de service.
Installez la bibliothèque cliente google-auth :
pipinstallgoogle-auth
Ajoutez le code suivant à un script Python :
importosimportgoogle.authfromgoogle.auth.transportimportrequestsimportrequestsasreqsos.environ["GOOGLE_APPLICATION_CREDENTIALS"]="PATH_TO_SERVICE_KEY"os.environ["GRPC_DEFAULT_SSL_ROOTS_FILE_PATH"]="CERT_NAME"# If you use a client library for your request,# you must include port :443 after the service endpoint# in the audience path.audience="https://ENDPOINT"creds,project_id=google.auth.default()print(project_id)creds=creds.with_gdch_audience(audience)deftest_get_token():sesh=reqs.Session()req=requests.Request(session=sesh)creds.refresh(req)print(creds.token)if__name__=="__main__":test_get_token()
Remplacez les éléments suivants :
PATH_TO_SERVICE_KEY : chemin d'accès au fichier JSON contenant les paires de clés de votre compte de service.
CERT_NAME : nom du fichier de certificat de l'autorité de certification, tel que org-1-trust-bundle-ca.cert. Vous n'avez besoin de cette valeur que si vous êtes dans un environnement de développement. Sinon, omettez-le.
ENDPOINT : point de terminaison de l'API que vous utilisez pour votre organisation. Pour en savoir plus, consultez l'état et les points de terminaison des services. Selon l'utilisation prévue du jeton d'authentification, vous devrez peut-être inclure le port après le point de terminaison du service dans le chemin d'accès à l'audience, comme suit :
Si vous utilisez une bibliothèque cliente pour votre requête, vous devez inclure le port :443 après le point de terminaison du service dans le chemin d'accès à l'audience. Par conséquent, le chemin d'accès audience dans le script doit être "https://ENDPOINT:443".
Si vous utilisez gRPC, curl ou des appels REST programmatiques pour votre demande, n'incluez pas le port. Par conséquent, le chemin d'accès audience dans le script doit être "https://ENDPOINT".
Enregistrez le script Python.
Exécutez le script Python pour récupérer le jeton :
pythonSCRIPT_NAME
Remplacez SCRIPT_NAME par le nom que vous avez donné à votre script Python, par exemple token.py.
Le résultat affiche le jeton d'authentification. Ajoutez le jeton à l'en-tête des requêtes de ligne de commande que vous effectuez, comme dans l'exemple suivant :
-H"Authorization: Bearer TOKEN"
Remplacez TOKEN par la valeur du jeton d'authentification affiché dans la sortie.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[[["\u003cp\u003eThis guide details the process of obtaining authentication tokens for accessing Vertex AI APIs on Google Distributed Cloud (GDC) air-gapped.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication can be performed using either a user account or a service account, each with its own distinct set of steps.\u003c/p\u003e\n"],["\u003cp\u003eTo obtain a user account token, you must log in to Distributed Cloud and run a command that includes the relevant service endpoint.\u003c/p\u003e\n"],["\u003cp\u003eTo obtain a service account token, you'll use a python script that sets the relevant environmental variables and uses the \u003ccode\u003egoogle-auth\u003c/code\u003e client library, referencing the service key and relevant endpoint.\u003c/p\u003e\n"],["\u003cp\u003eThe obtained authentication token is then added to the header of your API requests as an authorization bearer token.\u003c/p\u003e\n"]]],[],null,["# Authenticate Vertex AI API requests\n\nThis page describes how to authenticate calls to Vertex AI services on Google Distributed Cloud (GDC) air-gapped. You must set up token authentication to secure your requests to the Vertex AI API within your air-gapped applications. This process validates your API requests by providing your identity and authorizing your interactions.\n\n\u003cbr /\u003e\n\nThis page is for application developers within application operator groups responsible for setting up their application and development environments to enable AI features. For more information, see [Audiences for GDC air-gapped documentation](/distributed-cloud/hosted/docs/latest/gdch/resources/audiences).\n\nBefore you begin\n----------------\n\nYou must have your project set up for Vertex AI. For more information, see [Set up a project for Vertex AI](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-set-up-project).\n\n- Make sure to update your local trust store before you set up authentication in your development environment.\n\nAuthenticating to Vertex AI services\n------------------------------------\n\nInteractions with Vertex AI services are done through authentication tokens. Tokens are digital objects that verify your identity and authorization after you provide valid credentials. The token carries specific information about your account and the permissions it has to access and operate with services and resources.\n\nThere are two ways you can set up authentication:\n\n- [Authenticate with your user account](#authenticate-with-user-account)\n- [Authenticate with your service account](#authenticate-with-service-account)\n\n### Authenticate with your user account\n\nThe following guides you through getting an authentication token for your user account:\n\n1. Note [the endpoint of the API](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-api-status) you want to use.\n\n2. Gain access to the Vertex AI service or Generative AI model you want to use by granting your user account the corresponding role listed in [Prepare IAM permissions](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-ao-permissions).\n\n3. Sign in to Distributed Cloud with the user account you have to interact with the API:\n\n gdcloud auth login\n\n4. Get the authentication token:\n\n gdcloud auth print-identity-token --audiences=https://\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e with the API endpoint that you use for your organization. For more information, [view service status and endpoints](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-api-status).\n\n Depending on the intended use of the authentication token, you might need to include the port after the service endpoint in the audiences path as follows:\n - If you use a [client library](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-install-libraries) for your request, you must include port `:443` after the service endpoint in the audiences path. Therefore, the `--audiences` path in the command must be `https://`\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e`:443`.\n - If you use gRPC, `curl`, or programmatic REST calls for your request, don't include the port. Therefore, the `--audiences` path in the command must be `https://`\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e.\n\n The output displays the authentication token. Add the token to the header of the command-line requests you make, as in the following example: \n\n -H \"Authorization: Bearer \u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e\"\n\n Replace \u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e with the value for the authentication token that the output displays.\n\n### Authenticate with your service account\n\nThe following guides you through getting an authentication token for your service account:\n\n1. Note [the endpoint of the API](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-api-status) you want to use.\n\n2. [Set up the service account](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-set-up-project#set-up-service) you want to use to access the Vertex AI service or Generative AI model.\n\n3. Grant the service account the corresponding role listed in [Prepare IAM permissions](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-ao-permissions) to let it gain access to the service or model you want to use.\n\n4. [Get the service key pairs of your service account](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/iam/service-identities#list_credentials_for_service_accounts).\n\n5. Set the following environment variable:\n\n export GOOGLE_APPLICATION_CREDENTIALS=\u003cvar translate=\"no\"\u003ePATH_TO_SERVICE_KEY\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003ePATH_TO_SERVICE_KEY\u003c/var\u003e with the path to the JSON\n file that contains the key pairs of your service account.\n6. Install the `google-auth` client library:\n\n pip install google-auth\n\n7. Add the following code to a Python script:\n\n import os\n import google.auth\n from google.auth.transport import requests\n import requests as reqs\n\n os.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"] = \"\u003cvar translate=\"no\"\u003ePATH_TO_SERVICE_KEY\u003c/var\u003e\"\n os.environ[\"GRPC_DEFAULT_SSL_ROOTS_FILE_PATH\"] = \"\u003cvar translate=\"no\"\u003eCERT_NAME\u003c/var\u003e\"\n\n # If you use a client library for your request,\n # you must include port :443 after the service endpoint\n # in the audience path.\n audience = \"https://\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e\"\n\n creds, project_id = google.auth.default()\n print(project_id)\n creds = creds.with_gdch_audience(audience)\n\n def test_get_token():\n sesh = reqs.Session()\n req = requests.Request(session=sesh)\n creds.refresh(req)\n print(creds.token)\n\n if __name__==\"__main__\":\n test_get_token()\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePATH_TO_SERVICE_KEY\u003c/var\u003e: the path to the JSON file that contains the key pairs of your service account.\n - \u003cvar translate=\"no\"\u003eCERT_NAME\u003c/var\u003e: the name of the Certificate Authority (CA) certificate file, such as `org-1-trust-bundle-ca.cert`. You only need this value if you are in a development environment. Otherwise, omit it.\n - \u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e: the API endpoint that you use for your organization. For more information, [view service status and endpoints](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-api-status). Depending on the intended use of the authentication token, you might need to include the port after the service endpoint in the audience path as follows:\n\n - If you use a [client library](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vertex-ai-install-libraries) for your request, you must include port `:443` after the service endpoint in the audience path. Therefore, the `audience` path in the script must be `\"https://`\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e`:443\"`.\n - If you use gRPC, `curl`, or programmatic REST calls for your request, don't include the port. Therefore, the `audience` path in the script must be `\"https://`\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e`\"`.\n8. Save the Python script.\n\n9. Run the Python script to fetch the token:\n\n python \u003cvar translate=\"no\"\u003eSCRIPT_NAME\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003eSCRIPT_NAME\u003c/var\u003e with the name you gave to your Python script, such as `token.py`.\n\n The output displays the authentication token. Add the token to the header of the command-line requests you make, as in the following example: \n\n -H \"Authorization: Bearer \u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e\"\n\n Replace \u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e with the value for the authentication token that the output displays."]]