title: How can I log into gmail IMAP using MailKit
body: <p>I am trying to use MailKit (<a href="http://jstedfast.github.io/MailKit/docs/index.html" rel="nofollow">http://jstedfast.github.io/MailKit/docs/index.html</a>) to log into Gmail using oAuth. I am able to log into Google API using a refreshed AuthToken, but when I try to use the refreshed token in MailKit, I get an error "Invalid Credentials"</p>
  
  <p>Any clues??
  Thanks,
  Jeff</p>
  
  <p>Here is my code:</p>
  
  <pre><code> var secrets = new ClientSecrets()
          {
              ClientId = "xxx-yyy.apps.googleusercontent.com",
              ClientSecret = "xyzSecret"
          };
  
          IAuthorizationCodeFlow flow =
                  new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                  {
                      ClientSecrets = secrets,
                      Scopes = new string[] { GmailService.Scope.GmailReadonly }
                  });
  
          var tokenResponse = flow.RefreshTokenAsync("", connection.RefreshToken, CancellationToken.None).Result;
  
          using (var client = new MailKit.Net.Imap.ImapClient())
          {
              var credentials = new NetworkCredential("emailtoconnect@gmail.com", tokenResponse.AccessToken);
  
              client.Connect("imap.gmail.com", 993, true, CancellationToken.None);
              try
              {
                  client.Authenticate(credentials, CancellationToken.None);
              }
              catch (Exception ex)
              {
  
                  throw;
              }
  
  
          }
  </code></pre>