Remplacez YOUR_SERVICE_ACCOUNT_EMAIL par l'adresse e-mail de votre compte de service.
Remplacez YOUR_AUDIENCE par la valeur du champ aud envoyé par le service appelant.
Dans chaque méthode API pour laquelle vous souhaitez vérifier que l'authentification est appropriée, recherchez un élément User valide. S'il n'y en a pas, générez une erreur 401, comme indiqué dans cet exemple de définition de méthode :
user=endpoints.get_current_user()# If there's no user defined, the request was unauthenticated, so we# raise 401 Unauthorized.
Déployez l'API. Vous devez la redéployer à chaque ajout de nouveaux clients.
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 how to configure authentication for an API using a service account within a Google Cloud project.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication setup requires importing the App Engine Endpoints API and adding an issuer object to the API decorator, including details of the service account.\u003c/p\u003e\n"],["\u003cp\u003eEach API method should check for a valid user, raising a 401 error if no user is found, to ensure proper authentication.\u003c/p\u003e\n"],["\u003cp\u003eYou must redeploy the API after you configure the initial setup, and also after adding new clients to the API.\u003c/p\u003e\n"]]],[],null,["# Authenticating with a service account\n\nPrerequisites\n-------------\n\nThis page assumes that you have already:\n\n- [Created a Google Cloud project](/resource-manager/docs/creating-managing-projects).\n\n- [Added API management](/endpoints/docs/frameworks/java/adding-api-management).\n\nConfiguring authentication\n--------------------------\n\nTo authenticate with a service account:\n\n1. Import the App Engine Endpoints API in your API class:\n\n import endpoints\n\n2. Add an issuer object for the service account to the\n [API decorator](/endpoints/docs/frameworks/python/decorators-reference#defining_the_api_endpointsapi).\n For example:\n\n ```\n @endpoints.api(\n name='echo',\n version='v1',\n issuers={'serviceAccount': endpoints.Issuer(\n 'YOUR_SERVICE_ACCOUNT_EMAIL',\n 'https://www.googleapis.com/robot/v1/metadata/x509/YOUR_SERVICE_ACCOUNT_EMAIL')},\n audiences={'serviceAccount': ['YOUR_AUDIENCE']})\n ```\n - Replace \u003cvar translate=\"no\"\u003eecho\u003c/var\u003e with the name of your API.\n - Replace \u003cvar translate=\"no\"\u003ev1\u003c/var\u003e with your API version.\n - Replace \u003cvar translate=\"no\"\u003eYOUR_SERVICE_ACCOUNT_EMAIL\u003c/var\u003e with your service account email.\n - Replace \u003cvar translate=\"no\"\u003eYOUR_AUDIENCE\u003c/var\u003e with the value in the `aud` field sent by the calling service.\n3. In each API method where you want to check for proper authentication,\n check for a valid `User` and raise error `401`if there isn't one, as\n shown in this sample method definition:\n\n user = endpoints.get_current_user()\n # If there's no user defined, the request was unauthenticated, so we\n # raise 401 Unauthorized.\n\n4. [Deploy the API](/endpoints/docs/frameworks/python/test-deploy). You need to\n redeploy the API whenever you add new clients."]]