Les guides de démarrage rapide expliquent comment configurer et exécuter une application qui appelle une API Google Workspace. Ce guide de démarrage rapide utilise une approche d'authentification simplifiée qui convient à un environnement de test. Pour un environnement de production, nous vous recommandons de vous renseigner sur l'authentification et l'autorisation avant de choisir les identifiants d'accès adaptés à votre application.
Dans Apps Script, les démarrages rapides Google Workspace utilisent les services Google avancés pour appeler les API Google Workspace et gérer certains détails du flux d'authentification et d'autorisation.
/** * Lists all labels in the user's mailbox * @see https://developers.google.com/gmail/api/reference/rest/v1/users.labels/list */functionlistLabels(){try{// Gmail.Users.Labels.list() API returns the list of all Labels in user's mailboxconstresponse=Gmail.Users.Labels.list('me');if(!response||response.labels.length===0){// TODO (developer) - No labels are returned from the responseconsole.log('No labels found.');return;}// Print the Labels that are available.console.log('Labels:');for(constlabelofresponse.labels){console.log('- %s',label.name);}}catch(err){// TODO (developer) - Handle exception on Labels.list() APIconsole.log('Labels.list() API failed with error %s',err.toString());}}
Cliquez sur Enregistrer .
Cliquez sur Projet sans titre, saisissez Quickstart, puis cliquez sur Renommer.
Configurer le script
Activer l'API Gmail
Ouvrez le projet Apps Script.
Cliquez sur Éditeurcode.
À côté de Services, cliquez sur Ajouter un service add .
Sélectionnez l'API Gmail, puis cliquez sur Ajouter.
Exécuter l'exemple
Dans l'éditeur Apps Script, cliquez sur Exécuter.
La première fois que vous exécutez l'exemple, vous êtes invité à autoriser l'accès :
Cliquez sur Examiner les autorisations.
Choisissez un compte.
Cliquez sur Autoriser.
Le journal d'exécution du script s'affiche en bas de la fenêtre.
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,["Create a\n[Google Apps Script](/apps-script/overview)\nthat makes requests to the Gmail API.\n\nQuickstarts explain how to set up and run an app that calls a\nGoogle Workspace API. This quickstart uses a\nsimplified authentication approach that is appropriate for a testing\nenvironment. For a production environment, we recommend learning about\n[authentication and authorization](/workspace/guides/auth-overview)\nbefore\n[choosing the access credentials](/workspace/guides/create-credentials#choose_the_access_credential_that_is_right_for_you)\nthat are appropriate for your app.\n\nIn Apps Script, Google Workspace\nquickstarts use\n[Advanced Google services](/apps-script/guides/services/advanced) to call\nGoogle Workspace APIs and handle some details of the authentication\nand authorization flow.\n\nObjectives\n\n- Configure the environment.\n- Create and configure the script.\n- Run the script.\n\nPrerequisites\n\n\u003cbr /\u003e\n\n- A Google account with Gmail enabled.\n\n\u003cbr /\u003e\n\n- Access to Google Drive\n\nCreate the script\n\n1. Create a new script by going to [script.google.com/create](https://script.google.com/create).\n2. Replace the contents of the script editor with the following code:\n\n\ngmail/quickstart/quickstart.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/gmail/quickstart/quickstart.gs) \n\n```javascript\n/**\n * Lists all labels in the user's mailbox\n * @see https://developers.google.com/gmail/api/reference/rest/v1/users.labels/list\n */\nfunction listLabels() {\n try {\n // Gmail.Users.Labels.list() API returns the list of all Labels in user's mailbox\n const response = Gmail.Users.Labels.list('me');\n if (!response || response.labels.length === 0) {\n // TODO (developer) - No labels are returned from the response\n console.log('No labels found.');\n return;\n }\n // Print the Labels that are available.\n console.log('Labels:');\n for (const label of response.labels ) {\n console.log('- %s', label.name);\n }\n } catch (err) {\n // TODO (developer) - Handle exception on Labels.list() API\n console.log('Labels.list() API failed with error %s', err.toString());\n }\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n3. Click Save .\n4. Click **Untitled project** , type **Quickstart** , and click **Rename**.\n\n\u003cbr /\u003e\n\nConfigure the script\n\nEnable the Gmail API\n\nOpen the Apps Script project.\n\n1. Click **Editor** code.\n2. Next to **Services** , click Add a service add .\n3. Select Gmail API and click **Add**.\n\nRun the sample\n\nIn the Apps Script editor, click **Run**.\n\nThe first time you run the sample, it prompts you to authorize access:\n\n1. Click **Review permissions**.\n2. Choose an account.\n3. Click **Allow**.\n\nThe script's execution log appears at the bottom of the window. \ndone It worked! **Great!** Check out the further reading section below to learn more.\nwarning There was a problem **Bummer** , [let us know what went wrong](#). Check out our [troubleshooting](#troubleshooting) section below for some common errors and solutions. If you have found a bug in the code, [report the issue on GitHub](https://github.com/googleworkspace/apps-script-samples/issues) or submit a pull request.\n\nNext steps\n\n- [Google Apps Script Advanced Services documentation](/apps-script/guides/services/advanced)\n- [Try the Google Workspace APIs in the APIs explorer](/workspace/explore)\n- [Troubleshoot authentication and authorization issues](/workspace/gmail/api/troubleshoot-authentication-authorization)\n- [Gmail API reference documentation](/workspace/gmail/api/reference/rest)"]]