Skip to content

Commit

Permalink
Update FAQ regarding OAuth2 for GMail
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Oct 16, 2024
1 parent a3aadcc commit 5b4dc6d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 7 additions & 4 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ GMail Settings page and set your options to look like this:

### <a id="gmail-access">Q: How can I access GMail using MailKit?</a>

As of the end of May, 2022, Google no longer allows enabling "Less secure apps".
As of September 30th, 2024, authentication using only a username and password is [no longer supported by Google](https://support.google.com/accounts/answer/6010255?hl=en).

There are now only 2 options to choose from:

Expand Down Expand Up @@ -342,17 +342,20 @@ var codeFlow = new GoogleAuthorizationCodeFlow (new GoogleAuthorizationCodeFlow.
// Cache tokens in ~/.local/share/google-filedatastore/CredentialCacheFolder on Linux/Mac
DataStore = new FileDataStore ("CredentialCacheFolder", false),
Scopes = new [] { "https://mail.google.com/" },
ClientSecrets = clientSecrets
ClientSecrets = clientSecrets,
LoginHint = GMailAccount
});

// Note: For a web app, you'll want to use AuthorizationCodeWebApp instead.
var codeReceiver = new LocalServerCodeReceiver ();
var authCode = new AuthorizationCodeInstalledApp (codeFlow, codeReceiver);

var credential = await authCode.AuthorizeAsync (GMailAccount, CancellationToken.None);

if (credential.Token.IsExpired (SystemClock.Default))
if (credential.Token.IsStale)
await credential.RefreshTokenAsync (CancellationToken.None);

var oauth2 = new SaslMechanismOAuth2 (credential.UserId, credential.Token.AccessToken);
var oauth2 = new SaslMechanismOAuthBearer (credential.UserId, credential.Token.AccessToken);

using (var client = new ImapClient ()) {
await client.ConnectAsync ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
Expand Down
5 changes: 3 additions & 2 deletions GMailOAuth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var clientSecrets = new ClientSecrets {
};

var codeFlow = new GoogleAuthorizationCodeFlow (new GoogleAuthorizationCodeFlow.Initializer {
// Cache tokens in ~/.local/share/google-filedatastore/CredentialCacheFolder on Linux/Mac
DataStore = new FileDataStore ("CredentialCacheFolder", false),
Scopes = new [] { "https://mail.google.com/" },
ClientSecrets = clientSecrets,
Expand All @@ -89,7 +90,7 @@ var credential = await authCode.AuthorizeAsync (GMailAccount, CancellationToken.
if (credential.Token.IsStale)
await credential.RefreshTokenAsync (CancellationToken.None);

var oauth2 = new SaslMechanismOAuth2 (credential.UserId, credential.Token.AccessToken);
var oauth2 = new SaslMechanismOAuthBearer (credential.UserId, credential.Token.AccessToken);

using (var client = new ImapClient ()) {
await client.ConnectAsync ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
Expand Down Expand Up @@ -154,7 +155,7 @@ public async Task AuthenticateAsync ([FromServices] IGoogleAuthProvider auth)
GoogleCredential? googleCred = await auth.GetCredentialAsync ();
string token = await googleCred.UnderlyingCredential.GetAccessTokenForRequestAsync ();

var oauth2 = new SaslMechanismOAuth2 ("UserEmail", token);
var oauth2 = new SaslMechanismOAuthBearer ("UserEmail", token);

using var emailClient = new ImapClient ();
await emailClient.ConnectAsync ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
Expand Down

0 comments on commit 5b4dc6d

Please sign in to comment.