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.
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:
Register an OAuth app with Google Cloud and Microsoft Entra, or use a platform that manages this for you.
Send the user through the consent flow (authorization code flow with PKCE for public clients).
Request minimal scopes. Start with read-only if the agent only summarizes mail, then add send access only when needed.
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.
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.
Step
Build direct
Unified API (Nylas)
Provider apps
Register a Google Cloud app and a Microsoft Entra app
Same two apps, added to Nylas as connectors
Consent flow
Build and host two OAuth flows
One hosted flow for both providers
Tokens
Store, encrypt and refresh tokens for each provider
Nylas stores and refreshes tokens. The agent uses a grant ID
Mail API
Gmail API and Microsoft Graph, with two data models
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.
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.