This document defines the Simple Authentication and Security Layer
(SASL) XOAUTH2 mechanism for use
with the IMAP AUTHENTICATE, POP AUTH, and SMTP AUTH commands. This
mechanism allows the use of OAuth 2.0 access tokens to authenticate to a user's
Gmail account.
Using OAuth 2.0
Start by reading Using OAuth 2.0 to Access Google APIs. This document explains how OAuth 2.0 works, and the steps required to write a client.
To learn more, see the full Configure OAuth consent guide.
You can also browse the XOAUTH2 libraries and samples for working examples.
OAuth 2.0 scopes
The scope for IMAP, POP, and SMTP access is https://mail.google.com/. If you
request access to the full mail scope for your IMAP, POP, or SMTP app, it must
comply with the Google API Services User Data
Policy.
- To qualify for approval, your app must show full utilization of
https://mail.google.com/. - If your app doesn't require
https://mail.google.com/, migrate to the Gmail API and use more granular restricted scopes.
For more information, see Choose Gmail API scopes.
Domain-wide delegation for Google Workspace
To access Google Workspace users' mailboxes through IMAP with
Google Workspace domain-wide
delegation
and service accounts, you can
authorize your client with the
https://www.googleapis.com/auth/gmail.imap_admin scope instead.
When authorized with this scope, IMAP connections behave differently:
- IMAP displays all labels, even if users disabled "Show in IMAP" for the label in the Gmail settings.
- IMAP displays all messages, regardless of the "Folder Size Limits" setting in the Gmail settings.
SASL XOAUTH2
The SASL XOAUTH2 mechanism allows clients to send OAuth 2.0 access tokens to the server. The format uses encoded values shown in the following sections.
Initial client response
The SASL XOAUTH2 initial client response encodes the user and access token together in the following format:
base64("user=" USER "^Aauth=Bearer "
ACCESS_TOKEN "^A^A")
Use the base64 encoding mechanism defined in RFC
4648. ^A represents a
Control+A (\001).
For example, before base64-encoding, the initial client response might look like this (with a line break inserted for clarity):
user=someuser@example.com^Aauth=Bearer ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEu
Y29tCg^A^A
After base64-encoding, it becomes (with line breaks inserted for clarity):
dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQFhdXRoPUJlYXJlciB5YTI5LnZGOWRmdDRxbVRjMk52
YjNSbGNrQmhkSFJoZG1semRHRXVZMjl0Q2cBAQ==
Error response
An initial client response causing an error results in the server sending a challenge containing an error message in the following format:
base64(JSON_BODY)
JSON_BODY contains three values: status, schemes, and
scope. For example:
eyJzdGF0dXMiOiI0MDEiLCJzY2hlbWVzIjoiYmVhcmVyIG1hYyIsInNjb3BlIjoiaHR0cHM6Ly9t
YWlsLmdvb2dsZS5jb20vIn0K
After base64-decoding, this becomes (formatted for clarity):
{
"status": "401",
"schemes": "bearer",
"scope": "https://mail.google.com/"
}
The SASL protocol requires clients to send an empty response to this challenge.
IMAP protocol exchange
This section explains how to use SASL XOAUTH2 with the Gmail IMAP server.
Initial client response
To sign in with the SASL XOAUTH2 mechanism, the client invokes the
AUTHENTICATE command with the mechanism parameter of XOAUTH2, and the
initial client response as previously constructed. For example:
[connection begins]
C: C01 CAPABILITY
S: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA XLIST
CHILDREN XYZZY SASL-IR AUTH=XOAUTH2 AUTH=XOAUTH
S: C01 OK Completed
C: A01 AUTHENTICATE XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvb
QFhdXRoPUJlYXJlciB5YTI5LnZGOWRmdDRxbVRjMk52YjNSbGNrQmhkSFJoZG
1semRHRXVZMjl0Q2cBAQ==
S: A01 OK Success
[connection continues...]
Note the following details about the IMAP protocol exchange:
- For details on the IMAP
AUTHENTICATEcommand, see RFC 9051. - The
SASL-IRcapability lets the client send the initial response in the first line of theAUTHENTICATEcommand, completing authentication in a single round trip. For details onSASL-IR, see RFC 4959. - The
AUTH=XOAUTH2capability indicates that the server supports the SASL XOAUTH2 mechanism. The client activates this mechanism by passingXOAUTH2as the first argument to theAUTHENTICATEcommand. - The line breaks in the
AUTHENTICATEandCAPABILITYcommands are for readability only and aren't present in the actual command data. Pass the base64 argument as one continuous string without whitespace so the entireAUTHENTICATEcommand is a single line of text.
Error response
The server also returns authentication failures using the IMAP AUTHENTICATE
command:
[connection begins]
S: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA XLIST
CHILDREN XYZZY SASL-IR AUTH=XOAUTH2
S: C01 OK Completed
C: A01 AUTHENTICATE XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQ
FhdXRoPUJlYXJlciB5YTI5LnZGOWRmdDRxbVRjMk52YjNSbGNrQmhkSFJoZG1s
emRHRXVZMjl0Q2cBAQ==
S: + eyJzdGF0dXMiOiI0MDEiLCJzY2hlbWVzIjoiYmVhcmVyIG1hYyIsInNjb
3BlIjoiaHR0cHM6Ly9tYWlsLmdvb2dsZS5jb20vIn0K
C:
S: A01 NO SASL authentication failed
In this exchange, the client sends an empty response ("\r\n") to the challenge
containing the error message.
POP protocol exchange
This section explains how to use SASL XOAUTH2 with the Gmail POP server.
Initial client response
To sign in with the SASL XOAUTH2 mechanism, the client invokes the AUTH
command with the mechanism parameter of XOAUTH2, and the initial client
response as previously constructed. For example:
[connection begins]
C: AUTH XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQFhdXRoPUJlYX
JlciB5YTI5LnZGOWRmdDRxbVRjMk52YjNSbGNrQmhkSFJoZG1semRHRXVZMjl0
Q2cBAQ==
S: +OK Welcome.
[connection continues...]
Note the following details about the POP protocol exchange:
- For details on the POP
AUTHcommand, see RFC 5034. - The line breaks in the
AUTHcommand are for readability only and aren't present in the actual command data. Pass the base64 argument as one continuous string without whitespace so the entireAUTHcommand is a single line of text.
Error response
The server also returns authentication failures using the POP AUTH command:
[connection begins]
C: AUTH XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQFhdXRoPUJlY
XJlciB5YTI5LnZGOWRmdDRxbVRjMk52YjNSbGNrQmhkSFJoZG1semRHRXVZMj
l0Q2cBAQ==
S: + eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUi
OiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
SMTP protocol exchange
This section explains how to use SASL XOAUTH2 with the Gmail SMTP server.
Initial client response
To sign in with the XOAUTH2 mechanism, the client invokes the AUTH command
with the mechanism parameter of XOAUTH2, and the initial client response as
previously constructed. For example:
[connection begins]
S: 220 mx.google.com ESMTP 12sm2095603fks.9
C: EHLO sender.example.com
S: 250-mx.google.com at your service, [172.31.135.47]
S: 250-SIZE 35651584
S: 250-8BITMIME
S: 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
S: 250-ENHANCEDSTATUSCODES
S: 250 PIPELINING
C: AUTH XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQFhdXRoPUJlY
XJlciB5YTI5LnZGOWRmdDRxbVRjMk52YjNSbGNrQmhkSFJoZG1semRHRXVZMj
l0Q2cBAQ==
S: 235 2.7.0 Accepted
[connection continues...]
Note the following details about the SMTP protocol exchange:
- For details on the SMTP
AUTHcommand, see RFC 4954. - The line breaks in the
AUTHcommand are for readability only and aren't present in the actual command data. Pass the base64 argument as one continuous string without whitespace so the entireAUTHcommand is a single line of text.
Error response
The server also returns authentication failures using the SMTP AUTH command:
[connection begins]
S: 220 mx.google.com ESMTP 12sm2095603fks.9
C: EHLO sender.example.com
S: 250-mx.google.com at your service, [172.31.135.47]
S: 250-SIZE 35651584
S: 250-8BITMIME
S: 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
S: 250-ENHANCEDSTATUSCODES
S: 250 PIPELINING
C: AUTH XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQFhdXRoPUJlYXJl
ciB5YTI5LnZGOWRmdDRxbVRjMk52YjNSbGNrQmhkSFJoZG1semRHRXVZMjl0Q2cB
AQ==
S: 334 eyJzdGF0dXMiOiI0MDEiLCJzY2hlbWVzIjoiYmVhcmVyIG1hYyIsInNjb
3BlIjoiaHR0cHM6Ly9tYWlsLmdvb2dsZS5jb20vIn0K
C:
S: 535-5.7.1 Username and Password not accepted. Learn more at
S: 535 5.7.1 https://support.google.com/mail/?p=BadCredentials
hx9sm5317360pbc.68
[connection continues...]
In this exchange, the client sends an empty response ("\r\n") to the challenge
containing the error message.
Related topics
- OAUTH2: Using OAuth 2.0 to Access Google APIs
- SMTP: RFC 5321: Simple Mail Transfer Protocol
- IMAP: RFC 9051: Internet Message Access Protocol (IMAP) - Version 4rev2
- POP: RFC 1939: Post Office Protocol - Version 3
- POP3 SASL: RFC 5034: The Post Office Protocol (POP3) Simple Authentication and Security Layer (SASL) Authentication Mechanism
- SASL: RFC 4422: Simple Authentication and Security Layer (SASL)
- JSON: RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format
- BASE64: RFC 4648: The Base16, Base32, and Base64 Data Encodings
- SASL-IR: RFC 4959: IMAP Extension for Simple Authentication and Security Layer (SASL) Initial Client Response
- SMTP-AUTH: RFC 4954: SMTP Service Extension for Authentication