How to authenticate an AI agent with a user’s email account (Gmail and Outlook)

How to authenticate an AI agent with a user’s email account (Gmail and Outlook)

• 6 min read

Use OAuth 2.0 so the user grants your agent delegated, scoped, revocable access to their mailbox without sharing a password. Google requires the Google OAuth flow for Gmail and Microsoft requires the Microsoft identity platform for Outlook. A unified email API for AI agents, such as Nylas, handles both flows, token refresh and provider differences behind one set of endpoints. Request the narrowest scopes the agent needs.

This is the guide to authenticating AI agents with Gmail and Outlook using OAuth. For scope details, see the Google OAuth scopes and Microsoft OAuth scopes cookbooks. For token storage, see Store OAuth tokens securely.

What role does OAuth play when connecting AI agents to email?

OAuth lets an AI agent work in a user’s Gmail or Outlook mailbox without ever holding the password. The user signs in with Google or Microsoft, approves specific permissions, and the agent receives tokens it can use and the user can revoke. Platforms such as Nylas run this flow for both providers and manage the tokens.

  • Delegation: the agent acts for the user, within granted scopes.
  • Least privilege: scopes limit what the agent can read, send or modify.
  • Revocation: the user or admin can remove access at any time.
  • No password storage: your agent never holds the mailbox password.

What is the best approach for authenticating an AI agent with a user’s email?

The best approach is OAuth 2.0 with the narrowest scopes the agent needs, tokens encrypted per user and automatic refresh. You can register your own OAuth apps with Google and Microsoft, or use a unified API such as Nylas, which hosts the consent flow, stores and refreshes tokens, and gives the agent one set of endpoints for Gmail and Outlook. The steps:

  1. Register an OAuth app with Google Cloud and Microsoft Entra, or use a platform that manages this for you.
  2. Send the user through the consent flow (authorization code flow with PKCE for public clients).
  3. Request minimal scopes. Start with read-only if the agent only summarizes mail, then add send access only when needed.
  4. Store tokens securely. Encrypt refresh tokens and scope them per user.
  5. Refresh tokens automatically and handle revoked grants.
  6. Pass provider verification. Gmail sensitive scopes such as gmail.send need Google’s app verification. Restricted scopes such as gmail.readonly also need a security assessment if your servers store or transmit the data.

Gmail and Outlook name the same permissions differently, and Gmail’s read and send scopes need Google’s review before launch. Do not request Google’s broadest scope, https://mail.google.com/. Pick granular Gmail scopes for the actions the agent performs.

NeedGmailOutlook (Microsoft Graph)
Read mailgmail.readonly (restricted)Mail.Read
Send mailgmail.send (sensitive)Mail.Send
Refresh accessaccess_type=offline (returns a refresh token on the first code exchange)offline_access
ApprovalGoogle app verification, plus a security assessment for restricted scopesAdmin consent where tenants require it

Scope sources: Google OAuth 2.0 scopes and Microsoft identity platform scopes and permissions.

What’s the best way to connect an AI assistant to Gmail and Outlook at the same time?

To connect an AI assistant to Gmail and Outlook at the same time, use a unified email API such as Nylas instead of building separate Gmail and Outlook integrations. You run one OAuth flow, get one grant ID per user, and call the same endpoints for both providers. If your assistant only needs Gmail, the Gmail API alone can work.

Direct integration means two OAuth apps, two APIs (Gmail API and Microsoft Graph), two data models, two webhook systems and two sets of rate limits. A unified API normalizes these.

To implement OAuth for Gmail and Outlook in the same app, you either build and run each provider’s OAuth flow and API yourself, or run one flow through a unified API such as Nylas, using the same Google app you register.

StepBuild directUnified API (Nylas)
Provider appsRegister a Google Cloud app and a Microsoft Entra appSame two apps, added to Nylas as connectors
Consent flowBuild and host two OAuth flowsOne hosted flow for both providers
TokensStore, encrypt and refresh tokens for each providerNylas stores and refreshes tokens. The agent uses a grant ID
Mail APIGmail API and Microsoft Graph, with two data modelsOne set of endpoints for both
Change notificationsGmail push and Graph subscriptions, which both expire and need renewalNylas webhooks for both
Google verificationRequired for sensitive and restricted scopesStill required for your Google app

With Nylas, the agent authenticates a user once through a hosted or custom OAuth flow, then uses the same endpoints to list, read, send and search mail for either provider.

1. Redirect user to GET /v3/connect/auth
2. Exchange code with POST /v3/connect/token to get a grant ID
3. GET /v3/grants/{grant_id}/messages   # same call for Gmail or Outlook

What does a safe agent design look like?

A safe email agent keeps a human in the loop before sending, requests only the scopes each use case needs, and logs every action per grant. Use this checklist:

  • Keep a human approval step before the agent sends mail on the user’s behalf.
  • Log every agent action per grant.
  • Limit scopes per use case.
  • Handle expired or revoked grants gracefully.
  • Get webhooks for changes instead of polling.
  • Never write secrets to prompts or logs.

To expose list, search, send and reply as agent tools, see Email API tools for AI function calling.

Frequently asked questions

Can an AI agent log in with a user’s password?

It should not. Use OAuth, which Google and Microsoft both recommend for Gmail and Outlook, so the agent never stores the user’s password.

Do I need separate code for Gmail and Outlook?

Mostly no. With a unified API such as Nylas, the same endpoints list, read, send and search mail for both providers. You still register a Google app and a Microsoft app, and Google’s scope verification still applies.

Which scopes should I request?

Request the narrowest scopes the agent needs. For read-only summaries, use gmail.readonly on Gmail and Mail.Read on Outlook, and add gmail.send or Mail.Send only when the agent must send mail.

How do I keep access working over time?

Store refresh tokens securely and handle revocation events. For Google, request access_type=offline to receive a refresh token.

Should the agent have its own mailbox?

Some use cases suit an agent-owned inbox rather than a user’s. Nylas Agent Accounts give an AI agent its own email address and calendar.

Related resources

Meeting bot controls for the Nylas Notetaker API: dedupe, notices, branding and retention

Set per-customer meeting bot controls: dedupe, retention, recording notices, custom avatars and pausing dispatch with the Nylas Notetaker API.

Web Summit Lisbon 2026: Agent traffic is here and the web wasn’t built for it

Notes from Web Summit Lisbon 2026 on agent identity, AI-led buying, bot traffic and what SaaS is for when building is cheap.

Your product might be the vendor they replace

Two in three respondents say their company is considering replacing a vendor or tool with…