Skip to content

Commit 5b4dc6d

Browse files
committedOct 16, 2024·
Update FAQ regarding OAuth2 for GMail
1 parent a3aadcc commit 5b4dc6d

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed
 

‎FAQ.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ GMail Settings page and set your options to look like this:
291291

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

294-
As of the end of May, 2022, Google no longer allows enabling "Less secure apps".
294+
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).
295295

296296
There are now only 2 options to choose from:
297297

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

349+
// Note: For a web app, you'll want to use AuthorizationCodeWebApp instead.
348350
var codeReceiver = new LocalServerCodeReceiver ();
349351
var authCode = new AuthorizationCodeInstalledApp (codeFlow, codeReceiver);
352+
350353
var credential = await authCode.AuthorizeAsync (GMailAccount, CancellationToken.None);
351354

352-
if (credential.Token.IsExpired (SystemClock.Default))
355+
if (credential.Token.IsStale)
353356
await credential.RefreshTokenAsync (CancellationToken.None);
354357

355-
var oauth2 = new SaslMechanismOAuth2 (credential.UserId, credential.Token.AccessToken);
358+
var oauth2 = new SaslMechanismOAuthBearer (credential.UserId, credential.Token.AccessToken);
356359

357360
using (var client = new ImapClient ()) {
358361
await client.ConnectAsync ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);

‎GMailOAuth2.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var clientSecrets = new ClientSecrets {
7474
};
7575

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

92-
var oauth2 = new SaslMechanismOAuth2 (credential.UserId, credential.Token.AccessToken);
93+
var oauth2 = new SaslMechanismOAuthBearer (credential.UserId, credential.Token.AccessToken);
9394

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

157-
var oauth2 = new SaslMechanismOAuth2 ("UserEmail", token);
158+
var oauth2 = new SaslMechanismOAuthBearer ("UserEmail", token);
158159

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

0 commit comments

Comments
 (0)
Please sign in to comment.