Cuando una aplicación cliente incluye un JSON Web Token (JWT) en una solicitud a una API, el Extensible Service Proxy (ESP) valida el JWT antes de enviar la solicitud al backend de la API. En esta página se proporciona información para solucionar problemas si la validación de JWT falla y ESP devuelve un error en la respuesta al cliente. Consulta RFC 7519 para obtener más información sobre los JWTs.
Error: 401: Jwt issuer is not configured
Esto puede ocurrir al desplegar ESPv2 en Cloud Run si no se usa la marca --allow-unauthenticated en el comando gcloud run deploy.
Si no se usa la marca, Cloud Run intercepta y verifica el token JWT mediante el servidor IAM de <a=" docs="" managing-access"="" run="" securing="">control de acceso</a>, no mediante ESPv2. IAM puede usar una entidad emisora diferente a ESPv2.
</a=">
Error: BAD_FORMAT
Comprueba lo siguiente:
Asegúrate de que el JWT contenga un JSON válido.
Comprueba que el encabezado JWT tenga el campo "alg" y que esté definido como uno de los siguientes valores: "RS256", "HS256", "RS384", "HS384", "RS512" o "HS512".
Comprueba el tipo de datos de los siguientes campos (si están presentes) en la carga útil del JWT:
Las reclamaciones "iat" (emitida a las), "exp" (hora de vencimiento) y "nbf"(no antes de) son números mayores que 0 y no cadenas.
Los campos "sub" (asunto), "iss" (emisor) y "jti" (ID de JWT) son cadenas.
La reclamación "aud" (audiencia) es una cadena o un array de cadenas.
Asegúrate de que la carga útil del JWT incluya las siguientes reclamaciones: "sub" (sujeto), "iss" (emisor) y "aud" (audiencia).
A continuación, se muestra un ejemplo de token JWT decodificado que es válido:
Usa jwt.io para decodificar el JWT y asegúrate de que:
Existe la reclamación "exp" (hora de vencimiento).
El valor de la reclamación "exp" (tiempo de vencimiento) es una fecha y una hora futuras. La fecha y la hora actuales deben ser anteriores a la fecha y la hora de vencimiento que se indican en la reclamación "exp".
La reclamación "nbf" (no anterior) (si está presente) es una fecha y una hora del pasado. La fecha y la hora actuales deben ser posteriores o iguales a las que se indican en la reclamación de "nbf".
Error: UNKNOWN
Usa jwt.io para decodificar el JWT y asegúrate de que:
Si la reclamación "iss" (emisor) es una dirección de correo electrónico, las reclamaciones "sub" (sujeto) y "iss" deben ser las mismas.
De esta forma, se asegura que el JWT se emita automáticamente para los emisores de correo electrónico.
Error: KEY_RETRIEVAL_ERROR
Comprueba que el URI de la clave pública especificado en el campo x-google-jwks_uri de tu documento OpenAPI sea correcto y válido.
Error: Issuer not allowed
Comprueba que la reclamación "iss" (emisor) de tu token JWT coincida con el campo x-google-issuer de la sección securityDefinitions del objeto de seguridad de tu documento OpenAPI.
En tu documento de OpenAPI, comprueba que el objeto de seguridad esté habilitado para el método de la API invocado.
Consulta el archivo openapi.yaml de ejemplo para ver un ejemplo de cómo describir la seguridad a nivel de método mediante los objetos securityDefinition y security.
Error: Audience not allowed
Compara la reclamación "aud" (audiencia) de un token JWT para ver si coincide con el nombre del servicio de Endpoints, que corresponde al campo host del documento OpenAPI.
Si la reclamación "aud" y el nombre del servicio Endpoints son diferentes:
Comprueba que la reclamación "aud" del JWT coincida con uno de los valores x-google-audiences especificados en tu documento de OpenAPI.
Asegúrate de que x-google-audiences y x-google-issuer estén en el mismo objeto securityDefinitions de tu documento OpenAPI.
Si la reclamación "aud" y el nombre del servicio Endpoints son los mismos, el ESP valida la audiencia e ignora los valores de x-google-audiences de tu documento OpenAPI. Por ejemplo, si el nombre de tu servicio es "myservice.endpoints.example-project-12345.cloud.goog", un JWT con "aud" definido como "myservice.endpoints.example-project-12345.cloud.goog" o "https://myservice.endpoints.example-project-12345.cloud.goog" es una audiencia válida.
[[["Es fácil de entender","easyToUnderstand","thumb-up"],["Me ofreció una solución al problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Es difícil de entender","hardToUnderstand","thumb-down"],["La información o el código de muestra no son correctos","incorrectInformationOrSampleCode","thumb-down"],["Me faltan las muestras o la información que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2025-09-11 (UTC)."],[[["\u003cp\u003eThis document provides troubleshooting steps for JSON Web Token (JWT) validation failures encountered when using the Extensible Service Proxy (ESP) with an API.\u003c/p\u003e\n"],["\u003cp\u003eCommon JWT validation errors include \u003ccode\u003eBAD_FORMAT\u003c/code\u003e, \u003ccode\u003eTIME_CONSTRAINT_FAILURE\u003c/code\u003e, \u003ccode\u003eUNKNOWN\u003c/code\u003e, \u003ccode\u003eKEY_RETRIEVAL_ERROR\u003c/code\u003e, \u003ccode\u003eIssuer not allowed\u003c/code\u003e, and \u003ccode\u003eAudience not allowed\u003c/code\u003e, each with specific diagnostic instructions.\u003c/p\u003e\n"],["\u003cp\u003eWhen using Cloud Run, make sure the \u003ccode\u003e--allow-unauthenticated\u003c/code\u003e flag is used to prevent Cloud Run from intercepting and verifying the JWT token.\u003c/p\u003e\n"],["\u003cp\u003eThe document emphasizes checking the JWT's structure, ensuring the presence of required fields like \u003ccode\u003e"iss"\u003c/code\u003e, \u003ccode\u003e"sub"\u003c/code\u003e, and \u003ccode\u003e"aud"\u003c/code\u003e, and using \u003ca href=\"https://jwt.io/\"\u003ejwt.io\u003c/a\u003e to decode and examine the JWT's content.\u003c/p\u003e\n"],["\u003cp\u003eCorrect configuration of the \u003ccode\u003ex-google-jwks_uri\u003c/code\u003e, \u003ccode\u003ex-google-issuer\u003c/code\u003e, and \u003ccode\u003ex-google-audiences\u003c/code\u003e fields in the OpenAPI document is critical for successful JWT validation.\u003c/p\u003e\n"]]],[],null,["# Troubleshooting JWT validation\n\nOpenAPI \\| [gRPC](/endpoints/docs/grpc/troubleshoot-jwt \"View this page for the Cloud Endpoints gRPC docs\")\n\n\u003cbr /\u003e\n\nWhen a client application includes a JSON Web Token (JWT) in a request to an\nAPI, the [Extensible Service Proxy (ESP)](/endpoints/docs/openapi/glossary#extensible_service_proxy)\nvalidates the JWT before sending the request to the API\nbackend. This page provides troubleshooting information if the JWT validation\nfails and ESP returns an error in the response to the client. See\n[RFC 7519](https://tools.ietf.org/html/rfc7519) for more information\nabout JWTs.\n**Error: `401: Jwt issuer is not configured`**\n\nThis may happen when deploying ESPv2 in Cloud Run, the flag\n`--allow-unauthenticated` is not used in `gcloud run deploy` command.\nIf the flag is not used, the JWT token is intercepted\nand verified by Cloud Run access control IAM server and not by ESPv2. IAM may use a different issuer than ESPv2.\n**Error: `BAD_FORMAT`**\n\nCheck the\nfollowing:\n\n- Make sure the JWT contains valid JSON.\n- Check that the JWT header has the `\"alg\"` field and is set to one of the following: `\"RS256\"`, `\"HS256\"`, `\"RS384\"`, `\"HS384\"`, `\"RS512\"`, or `\"HS512\"`\n- Check the data type of the following fields (if they are present) in the JWT payload:\n - The `\"iat\"` (issued at), `\"exp\"` (expiration time), and `\"nbf\"`(not before) claims are numbers greater than 0 and not strings.\n - The `\"sub\"` (subject), `\"iss\"` (issuer), and `\"jti\"` (JWT ID) fields are strings.\n - The `\"aud\"` (audience) claim is either a string or an array of strings.\n- Ensure that the following claims are present in the JWT payload: `\"sub\"` (subject), `\"iss\"` (issuer), and `\"aud\"` (audience).\n\nThe following is an example of a decoded JWT token that is valid: \n\n```\n{\n \"alg\": \"RS256\",\n \"typ\": \"JWT\",\n \"kid\": \"42ba1e234ac91ffca687a5b5b3d0ca2d7ce0fc0a\"\n}\n\nPayload:\n{\n \"iss\": \"myservice@myproject.iam.gserviceaccount.com\",\n \"iat\": 1493833746,\n \"aud\": \"myservice.appspot.com\",\n \"exp\": 1493837346,\n \"sub\": \"myservice@myproject.iam.gserviceaccount.com\"\n}\n```\n**Error: `TIME_CONSTRAINT_FAILURE`**\n\nUse [jwt.io](https://jwt.io/) to decode the JWT and make sure that:\n\n- The `\"exp\"` (expiration time) claim exists.\n- The `\"exp\"` (expiration time) claim value is a date and time in the future. The current date and time must be before the expiration date and time listed in the `\"exp\"` claim.\n- The `\"nbf\"` (not before) claim (If present) is a date and time in the past. The current date and time must be after or equal to the date and time listed in the `\"nbf\"` claim.\n\n**Error: `UNKNOWN`**\n\nUse [jwt.io](https://jwt.io/) to decode the JWT and ensure that:\n\n- If the `\"iss\"` (issuer) claim is an email address, then the `\"sub\"` (subject) and `\"iss\"` claims should be the same. This is to ensure that for e-mail issuers, the JWT is self issued.\n\n**Error: `KEY_RETRIEVAL_ERROR`**\n\n- Check that the public key URI specified in the [`x-google-jwks_uri`](/endpoints/docs/openapi/openapi-extensions#x-google-jwks_uri) field in your OpenAPI document is correct and valid.\n\n**Error: `Issuer not allowed`**\n\n- Check that the `\"iss\"` (issuer) claim in your JWT token matches the\n [`x-google-issuer`](/endpoints/docs/openapi/openapi-extensions#x-google-issuer)\n field in the `securityDefinitions` section of the security object in your\n OpenAPI document.\n\n- In your OpenAPI document, check that the security object is\n enabled for the API method invoked.\n\nSee the\n[sample openapi.yaml](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/endpoints/getting-started/openapi.yaml)\nfile for an example of how to describe security at the method level by using\n`securityDefinition` and `security` objects.\n\n**Error: `Audience not allowed`**\n\nCompare the `\"aud\"` (audience) claim in a JWT token to see if it matches the\nEndpoints service name, which corresponds to the `host` field in\nthe OpenAPI document.\n\nIf the `\"aud\"` claim and the Endpoints service name are\ndifferent:\n\n- Check that the `\"aud\"` claim in the JWT matches one of the\n `x-google-audiences` values specified in your OpenAPI document.\n\n- Make sure that the `x-google-audiences` and `x-google-issuer` are in the same\n `securityDefinitions` object in your OpenAPI document.\n\nIf the `\"aud\"` claim and the Endpoints service name are the same,\nthe ESP validates the audience and\nignores the\n[`x-google-audiences`](/endpoints/docs/openapi/openapi-extensions#x-google-audiences)\nvalues in your OpenAPI document. For example, if your service name\nis `\"myservice.endpoints.example-project-12345.cloud.goog\"`, then a JWT with\n`\"aud\"` set to `\"myservice.endpoints.example-project-12345.cloud.goog\"` or\n`\"https://myservice.endpoints.example-project-12345.cloud.goog\"` is a valid\naudience."]]