enough_mail
enough_mail copied to clipboard
Credentials using Oauth 2
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.
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