In some cases, the user may be pre-authenticated by the server. In
this case, the method will fail with the last error set to
ST_ERROR_ALREADY_AUTHENTICATED. If a particular authentication method
is not supported by the server, the last error will be set to
ST_ERROR_INVALID_AUTHENTICATION_TYPE. For compatibility with the
greatest number of servers, it is recommended that you use
IMAP_AUTH_LOGIN as the authentication method.
You should only use an OAuth 2.0 authentication method if
you understand the process of how to request the access token. Obtaining
an access token requires registering your application with the mail
service provider (e.g.: Microsoft or Google), getting a unique
client ID associated with your application and then requesting the
access token using the appropriate scope for the service. Obtaining
the initial token will typically involve interactive confirmation
on the part of the user, requiring they grant permission to your
application to access their mail account.
The IMAP_AUTH_XOAUTH2 and IMAP_AUTH_BEARER authentication methods are
similar, but they are not interchangeable. Both use an OAuth 2.0 bearer
token to authenticate the client session, but they differ in how the
token is presented to the server. It is currently preferable to use the
XOAUTH2 method because it is more widely available and some service
providers do not yet support the OAUTHBEARER method.
Your application should not store an OAuth 2.0 bearer token for later
use. They have a relatively short lifespan, typically about an hour, and
are designed to be used with that session. You should specify offline
access as part of the OAuth 2.0 scope if necessary and store the refresh
token provided by the service. The refresh token has a much longer
validity period and can be used to obtain a new bearer token when needed.