Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Un Bearer Token est défini dans l'en-tête Authorization de chaque requête HTTP d'action dans l'application. Exemple :
POST/approve?expenseId=abc123HTTP/1.1Host:your-domain.comAuthorization:Bearer AbCdEf123456Content-Type:application/x-www-form-urlencodedUser-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)confirmed=Approved
Dans l'exemple ci-dessus, la chaîne "AbCdEf123456" est le jeton d'autorisation du porteur.
Il s'agit d'un jeton cryptographique produit par Google.
Tous les jetons de support envoyés avec des actions ont le champ azp (partie autorisée) défini sur gmail@system.gserviceaccount.com, avec le champ audience spécifiant le domaine de l'expéditeur sous la forme d'une URL https://. Par exemple, si l'adresse e-mail est noreply@example.com, l'audience est https://example.com.
Si vous utilisez des jetons du porteur, vérifiez que la requête provient de Google et qu'elle est destinée au domaine de l'expéditeur. Si le jeton n'est pas validé, le service doit répondre à la requête avec un code de réponse HTTP 401 (Unauthorized).
Les jetons de support font partie de la norme OAuth V2 et sont largement adoptés par les API Google.
Vérifier les jetons de support
Nous encourageons les services à utiliser la bibliothèque cliente Google API Open Source pour valider les jetons Bearer :
importjava.io.IOException;importjava.security.GeneralSecurityException;importjava.util.Collections;importcom.google.api.client.googleapis.auth.oauth2.GoogleIdToken;importcom.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;importcom.google.api.client.http.apache.ApacheHttpTransport;importcom.google.api.client.json.jackson2.JacksonFactory;publicclassTokenVerifier{// Bearer Tokens from Gmail Actions will always be issued to this authorized party.privatestaticfinalStringGMAIL_AUTHORIZED_PARTY="gmail@system.gserviceaccount.com";// Intended audience of the token, based on the sender's domainprivatestaticfinalStringAUDIENCE="https://example.com";publicstaticvoidmain(String[]args)throwsGeneralSecurityException,IOException{// Get this value from the request's Authorization HTTP header.// For example, for "Authorization: Bearer AbCdEf123456" use "AbCdEf123456"StringbearerToken="AbCdEf123456";GoogleIdTokenVerifierverifier=newGoogleIdTokenVerifier.Builder(newApacheHttpTransport(),newJacksonFactory()).setAudience(Collections.singletonList(AUDIENCE)).build();GoogleIdTokenidToken=verifier.verify(bearerToken);if(idToken==null||!idToken.getPayload().getAuthorizedParty().equals(GMAIL_AUTHORIZED_PARTY)){System.out.println("Invalid token");System.exit(-1);}// Token originates from Google and is targeted to a specific client.System.out.println("The token is valid");System.out.println("Token details:");System.out.println(idToken.getPayload().toPrettyString());}}
Python
importsysfromoauth2clientimportclient# Bearer Tokens from Gmail Actions will always be issued to this authorized party.GMAIL_AUTHORIZED_PARTY='gmail@system.gserviceaccount.com'# Intended audience of the token, based on the sender's domainAUDIENCE='https://example.com'try:# Get this value from the request's Authorization HTTP header.# For example, for "Authorization: Bearer AbCdEf123456" use "AbCdEf123456"bearer_token='AbCdEf123456'# Verify valid token, signed by google.com, intended for a third party.token=client.verify_id_token(bearer_token,AUDIENCE)print('Token details: %s'%token)iftoken['azp']!=GMAIL_AUTHORIZED_PARTY:sys.exit('Invalid authorized party')except:sys.exit('Invalid token')# Token originates from Google and is targeted to a specific client.print('The token is valid')
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/08/29 (UTC).
[null,null,["Dernière mise à jour le 2025/08/29 (UTC)."],[],[],null,["# Verify Bearer Tokens\n\n| **Note:** Bearer tokens in authorization headers are not sent by default. If you require a bearer token token to be sent, request it when [registering with Google](/workspace/gmail/markup/registering-with-google).\n\nA `Bearer Token` is set in the `Authorization` header of every In-App Action HTTP Request. For example: \n\n POST /approve?expenseId=abc123 HTTP/1.1\n Host: your-domain.com\n Authorization: Bearer AbCdEf123456\n Content-Type: application/x-www-form-urlencoded\n User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)\n\n confirmed=Approved\n\nThe string \"AbCdEf123456\" in the example above is the bearer authorization token.\nThis is a cryptographic token produced by Google.\nAll bearer tokens sent with actions have the `azp` (authorized party) field as\n`gmail@system.gserviceaccount.com`, with the `audience` field specifying the sender domain as a URL of the form\n`https://`. For example, if the email is from `noreply@example.com`, the\naudience is `https://example.com`.\n\nIf using bearer tokens, verify that the request is coming from Google\nand is intended for the the sender domain. If the token doesn't verify, the service should\nrespond to the request with an HTTP response code `401 (Unauthorized)`.\n\nBearer Tokens are part of the OAuth V2 standard and widely adopted by Google APIs.\n\nVerifying Bearer Tokens\n-----------------------\n\nServices are encouraged to use the open source Google API Client library to verify Bearer tokens:\n\n- **Java** : \u003chttps://github.com/google/google-api-java-client\u003e\n- **Python** : \u003chttps://github.com/google/google-api-python-client\u003e\n- **.NET** : \u003chttps://github.com/google/google-api-dotnet-client\u003e\n\n### Java\n\n import java.io.IOException;\n import java.security.GeneralSecurityException;\n import java.util.Collections;\n\n import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;\n import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;\n import com.google.api.client.http.apache.ApacheHttpTransport;\n import com.google.api.client.json.jackson2.JacksonFactory;\n\n public class TokenVerifier {\n // Bearer Tokens from Gmail Actions will always be issued to this authorized party.\n private static final String GMAIL_AUTHORIZED_PARTY = \"gmail@system.gserviceaccount.com\";\n\n // Intended audience of the token, based on the sender's domain\n private static final String AUDIENCE = \"https://example.com\";\n\n public static void main(String[] args) throws GeneralSecurityException, IOException {\n // Get this value from the request's Authorization HTTP header.\n // For example, for \"Authorization: Bearer AbCdEf123456\" use \"AbCdEf123456\"\n String bearerToken = \"AbCdEf123456\";\n\n GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new ApacheHttpTransport(), new JacksonFactory())\n .setAudience(Collections.singletonList(AUDIENCE))\n .build();\n\n GoogleIdToken idToken = verifier.verify(bearerToken);\n if (idToken == null || !idToken.getPayload().getAuthorizedParty().equals(GMAIL_AUTHORIZED_PARTY)) {\n System.out.println(\"Invalid token\");\n System.exit(-1);\n }\n\n // Token originates from Google and is targeted to a specific client.\n System.out.println(\"The token is valid\");\n\n System.out.println(\"Token details:\");\n System.out.println(idToken.getPayload().toPrettyString());\n }\n }\n\n### Python\n\n import sys\n\n from oauth2client import client\n\n # Bearer Tokens from Gmail Actions will always be issued to this authorized party.\n GMAIL_AUTHORIZED_PARTY = 'gmail@system.gserviceaccount.com'\n\n # Intended audience of the token, based on the sender's domain\n AUDIENCE = 'https://example.com'\n\n try:\n # Get this value from the request's Authorization HTTP header.\n # For example, for \"Authorization: Bearer AbCdEf123456\" use \"AbCdEf123456\"\n bearer_token = 'AbCdEf123456'\n\n # Verify valid token, signed by google.com, intended for a third party.\n token = client.verify_id_token(bearer_token, AUDIENCE)\n print('Token details: %s' % token)\n\n if token['azp'] != GMAIL_AUTHORIZED_PARTY:\n sys.exit('Invalid authorized party')\n except:\n sys.exit('Invalid token')\n\n # Token originates from Google and is targeted to a specific client.\n print('The token is valid')"]]