enough_mail icon indicating copy to clipboard operation
enough_mail copied to clipboard

Credentials using Oauth 2

Open thirtyninetythree opened this issue 2 years ago • 1 comments

How to use google sign in and enough mail to get emails from gmail without the user having to sign in with their raw credentials.

thirtyninetythree avatar Jun 17 '22 10:06 thirtyninetythree

Assuming you have a valid OAuth token, you can sign in with that token. Example for low level API:

String accessToken = await _retrieveAndRefreshAccessToken();
await imapClient.authenticateWithOAuth2(email, accessToken);

Example for high level API:

final OauthToken token = await _retrieveOauthToken();
final account = MailAccount.fromDiscoveredSettingsWithAuth(
  'acccount name',
  email, 
  OauthAuthentication(email, token), 
  discoveredClientConfig,
);
final mailClient = MailClient(
  account, 
  refresh: (mailClient, expiredToken) async {
     // TODO refresh token
     return expiredToken;
  }
}

With the above high level code you only need to fill in the google specific ways to get your token and then refresh your expired token. You can learn about gmail specific details here: https://developers.google.com/gmail/imap/xoauth2-protocol

robert-virkus avatar Jun 30 '22 15:06 robert-virkus