Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Auf dieser Seite wird beschrieben, wie Sie in Ihrer API mithilfe von Cloud Endpoints Frameworks die Unterstützung der Nutzerauthentifizierung von Clientanwendungen hinzufügen. Derzeit werden Android- und JavaScript-Clients unterstützt.
Endpoints Frameworks unterstützt die Nutzerauthentifizierung von Clientanwendungen, die eine der folgenden Methoden verwenden:
Unabhängig davon, welche Authentifizierungsmethode Sie verwenden, müssen Sie in jeder API-Methode, in der Sie die ordnungsgemäße Authentifizierung prüfen möchten, wie in den folgenden Abschnitten beschrieben, nach einem gültigen User suchen:
Wenn Sie in Ihrem Client JWT verwenden, um authentifizierte Anfragen an die API zu senden, muss das JWT im Autorisierungsheader der HTTP-Anfrage enthalten sein. Das JWT sollte folgende erforderlichen Anforderungen erfüllen:
iss
sub
aud
iat
exp
Mit Firebase Auth authentifizieren
So unterstützen Sie Aufrufe von Clients, die Firebase Auth verwenden:
Importieren Sie die App Engine Cloud Endpoints API in Ihre API-Klasse:
importendpoints
Fügen Sie dem API-Decorator ein Firebase-Ausstellerobjekt für jeden Client hinzu.
Beispiel:
Ersetzen Sie YOUR_API_NAME durch den Namen der API.
Ersetzen Sie VERSION_NUMBER durch Ihre API-Version, z. B. v1.
Ersetzen Sie YOUR_PROJECT_ID durch die Google CloudProjekt-ID des Kunden.
Prüfen Sie in jeder API-Methode, mit der Sie eine ordnungsgemäße Authentifizierung gewährleisten möchten, ob ein gültiger User vorhanden ist. Wenn dies nicht der Fall ist, geben Sie wie in der folgenden Methodendefinition beispielhaft gezeigt den Fehler error 401 aus:
user=endpoints.get_current_user()# If there's no user defined, the request was unauthenticated, so we# raise 401 Unauthorized.
Firebase-Authentifizierung zu einem Client hinzufügen
Sie können Ihren Code um die Firebase-Authentifizierung erweitern, wie in der Firebase-Dokumentation beschrieben. Dem Client muss ein Google Cloud -Projekt zugewiesen sein und die Projekt-ID muss in der Konfiguration des Firebase-Ausstellers der API aufgeführt sein.
Mit Auth0 authentifizieren
So unterstützen Sie Aufrufe von Clients, die Auth0 verwenden:
Importieren Sie die App Engine Endpoints API in Ihre API-Klasse:
importendpoints
Fügen Sie dem API-Decorator ein Auth0-Ausstellerobjekt für jeden Client hinzu.
Beispiel:
Ersetzen Sie YOUR_API_NAME durch den Namen der API.
Ersetzen Sie VERSION_NUMBER durch Ihre API-Version, z. B. v1.
Ersetzen Sie YOUR_ACCOUNT_NAME durch den für den Client verwendeten Auth0-Kontonamen.
Prüfen Sie in jeder API-Methode, mit der Sie eine ordnungsgemäße Authentifizierung gewährleisten möchten, ob ein gültiger User vorhanden ist. Wenn dies nicht der Fall ist, geben Sie wie in der folgenden Methodendefinition beispielhaft gezeigt den Fehler error 401 aus:
user=endpoints.get_current_user()# If there's no user defined, the request was unauthenticated, so we# raise 401 Unauthorized.
Auth0-Authentifizierung zu einem Client hinzufügen
Sie können Ihren Code um die Auth0-Authentifizierung erweitern, wie in der Auth0-Dokumentation beschrieben. Der Client muss in der Auth0-Ausstellerkonfiguration der API aufgeführt sein.
Mit Google-ID-Tokens authentifizieren
So unterstützen Sie Aufrufe von Clients, die für die Authentifizierung Google-ID-Tokens verwenden:
Fordern Sie für jede Clientanwendung eine OAuth 2-Client-ID an. Der Inhaber der Clientanwendung muss die Client-ID in der Google Cloud Console generieren. Eine Anleitung hierzu finden Sie unter Client-IDs erstellen.
Importieren Sie die App Engine Endpoints-API in Ihre API-Klasse:
importendpoints
Geben Sie in den allowed_client_ids alle Client-IDs an, denen Sie Zugriff auf Ihre API gewähren möchten. Geben Sie außerdem die Client-IDs von Android-Clients im Feld audiences des API-Decorators an.
Beispiel:
@endpoints.api(
name='YOUR_API_NAME',
version='VERSION_NUMBER',
allowed_client_ids=ALLOWED_CLIENT_IDS,
audiences=[ANDROID_AUDIENCE])
class AuthedGreetingApi(remote.Service):
# ...
Ersetzen Sie ALLOWED_CLIENT_IDS durch die Liste der OAuth 2-Client-IDs, die von jedem Clientprojekt generiert wurden, und ersetzen Sie ANDROID_AUDIENCE durch die Liste der Android-Webclient-IDs. Die Webclient-ID ist die Client-ID mit dem Zusatz .apps.googleusercontent.com. Beispiel: YOUR_CLIENT_ID.apps.googleusercontent.com.
Prüfen Sie in jeder API-Methode, mit der Sie eine ordnungsgemäße Authentifizierung gewährleisten möchten, ob ein gültiger User vorhanden ist. Wenn dies nicht der Fall ist, geben Sie wie in der folgenden Methodendefinition beispielhaft gezeigt den Fehler error 401 aus:
user=endpoints.get_current_user()# If there's no user defined, the request was unauthenticated, so we# raise 401 Unauthorized.
Hintergrundinformationen zur Nutzerauthentifizierung und zum Unterschied zur API-Schlüsselautorisierung finden Sie unter API-Schlüssel effizient nutzen.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\u003cp\u003eThis guide outlines how to implement user authentication in your API using Cloud Endpoints Frameworks, currently supporting Android and JavaScript clients.\u003c/p\u003e\n"],["\u003cp\u003eEndpoints Frameworks enables user authentication from client applications via Firebase Auth, Auth0, or Google ID tokens.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure proper authentication, you must check for a valid \u003ccode\u003eUser\u003c/code\u003e within each API method, regardless of the chosen authentication method.\u003c/p\u003e\n"],["\u003cp\u003eImplementing authentication involves importing the App Engine Endpoints API, configuring issuer objects in the API decorator, and deploying the updated API.\u003c/p\u003e\n"],["\u003cp\u003eIf you use JWT in your client to send authenticated requests to the API, the JWT must be in the authorization header of a HTTP request and contain the claims \u003ccode\u003eiss\u003c/code\u003e, \u003ccode\u003esub\u003c/code\u003e, \u003ccode\u003eaud\u003c/code\u003e, \u003ccode\u003eiat\u003c/code\u003e, and \u003ccode\u003eexp\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Authenticating users\n\nThis page describes how to add support in your API for user authentication from\nclient applications by using Cloud Endpoints Frameworks. Note that\nAndroid and JavaScript clients are currently supported.\n\nEndpoints Frameworks supports user authentication from client\napplications that use any of the following methodologies:\n\n- [Firebase Auth](https://firebase.google.com/docs/auth/)\n- [Auth0](http://auth0.com)\n- [Google ID tokens](/endpoints/docs/frameworks/glossary#google_id_token)\n\nNo matter which authentication method you use, in each API method where you want\nto check for proper authentication, you must check for a valid `User` as\ndescribed in the following sections:\n\n- [Authenticating with Firebase Auth](#authenticating_with_firebase_auth)\n- [Authenticating with Auth0](#authenticating_with_auth0)\n- [Authenticating with Google ID tokens](#google-id-tokens)\n\nPrerequisites\n-------------\n\nThis page assumes that you have already:\n\n- Created a\n [Google Cloud project](/resource-manager/docs/creating-managing-projects).\n\n- [Added API management](/endpoints/docs/frameworks/java/adding-api-management).\n\n \u003cbr /\u003e\n\n- If you use JWT in your client to send authenticated requests to the API, the JWT must be in the authorization header of a HTTP request. The JWT should have the following required claims:\u003cbr /\u003e\n\n - `iss`\n - `sub`\n - `aud`\n - `iat`\n - `exp`\n\n \u003cbr /\u003e\n\nAuthenticating with Firebase Auth\n---------------------------------\n\nTo support calls from clients that use Firebase Auth:\n\n1. Import the App Engine Cloud Endpoints API in your API class:\n\n import endpoints\n\n2. Add a Firebase issuer object for each client 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='YOUR_API_NAME',\n version='VERSION_NUMBER',\n issuers={'firebase': endpoints.Issuer(\n 'https://securetoken.google.com/YOUR_PROJECT_ID,\n 'https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com')})\n ```\n - Replace \u003cvar translate=\"no\"\u003eYOUR_API_NAME\u003c/var\u003e with the name of your API.\n - Replace \u003cvar translate=\"no\"\u003eVERSION_NUMBER\u003c/var\u003e with your API version, for example, `v1`.\n - Replace code\\\u003e\u003cvar translate=\"no\"\u003eYOUR_PROJECT_ID\u003c/var\u003e with the Google Cloud project ID of the client.\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 Endpoints API](/endpoints/docs/frameworks/python/test-deploy).\n You need to redeploy the Endpoints API whenever you add new\n clients.\n\n### Adding Firebase authentication to a client\n\nYou can add Firebase authentication to your code as described in the\n[Firebase](https://firebase.google.com/docs/auth/)\ndocumentation. The client must have a Google Cloud project associated with\nit, and the project ID must be listed in the API's Firebase issuer\nconfiguration.\n\nAuthenticating with Auth0\n-------------------------\n\nTo support calls from clients that use Auth0:\n\n1. Import the App Engine Endpoints API in your API class:\n\n import endpoints\n\n2. Add an Auth0 issuer object for each client to\n the\n [API decorator](/endpoints/docs/frameworks/python/decorators-reference#defining_the_api_endpointsapi).\n For example:\n\n ```\n @endpoints.api(\n name='YOUR_API_NAME',\n version='VERSION_NUMBER',\n issuers={'auth0': endpoints.Issuer(\n 'https://YOUR_ACCOUNT_NAME.auth0.com',\n 'https://YOUR_ACCOUNT_NAME.auth0.com/.well-known/jwks.json')})\n ```\n - Replace \u003cvar translate=\"no\"\u003eYOUR_API_NAME\u003c/var\u003e with the name of your API.\n - Replace \u003cvar translate=\"no\"\u003eVERSION_NUMBER\u003c/var\u003e with your API version, for example, `v1`.\n - Replace \u003cvar translate=\"no\"\u003eYOUR_ACCOUNT_NAME\u003c/var\u003e with the Auth0 account name used for the client.\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\n need to redeploy the API whenever you add new clients.\n\n### Adding Auth0 authentication to a client\n\nYou can add Auth0 authentication to your code as described in the\n[Auth0](http://auth0.com)\ndocumentation. The client must be listed in the API's Auth0 issuer configuration.\n\nAuthenticating with Google ID tokens\n------------------------------------\n\nTo support calls from clients that authenticate using Google ID tokens:\n\n1. Obtain an OAuth 2 client ID for each client application. The client\n application owner must generate the client ID from the Google Cloud console. For\n instructions, see\n [Creating client IDs](/endpoints/docs/frameworks/python/creating-client-ids).\n\n2. Import the App Engine Endpoints API in your API class:\n\n import endpoints\n\n3. Specify all of the client IDs you want to grant access to your API\n in the `allowed_client_ids`, and also specify client IDs belonging to Android\n clients in the`audiences` field in the\n [API decorator](/endpoints/docs/frameworks/python/decorators-reference#defining_the_api_endpointsapi).\n For example:\n\n ```\n @endpoints.api(\n name='YOUR_API_NAME',\n version='VERSION_NUMBER',\n allowed_client_ids=ALLOWED_CLIENT_IDS,\n audiences=[ANDROID_AUDIENCE])\n class AuthedGreetingApi(remote.Service):\n # ...\n ```\n\n Replace \u003cvar translate=\"no\"\u003eALLOWED_CLIENT_IDS\u003c/var\u003e with the list of OAuth 2\n client IDs generated from each client's project, and replace\n \u003cvar translate=\"no\"\u003eANDROID_AUDIENCE\u003c/var\u003e with the list of Android web client\n IDs. The web client ID is the client ID with `.apps.googleusercontent.com`\n appended, for example:\n \u003cvar translate=\"no\"\u003eYOUR_CLIENT_ID\u003c/var\u003e`.apps.googleusercontent.com`.\n4. 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\n5. [Deploy the Endpoints API](/endpoints/docs/frameworks/python/test-deploy).\n You need to redeploy the Endpoints API whenever you add new\n clients.\n\n### Adding Google ID token authentication to a client\n\nFor information on adding authentication code to clients, see the following:\n\n- [Android app](/endpoints/docs/frameworks/python/consume_android)\n- [Python client](/endpoints/docs/frameworks/python/access_from_python)\n\nWhat's next\n-----------\n\nFor background information about user authentication and how it differs from\nAPI key authorization, see\n[When and why to use API keys](/endpoints/docs/frameworks/python/when-why-api-key)."]]