Search code examples
vb.netgmail-apigoogle-oauthgmail-imapchilkat

Google Email Login with OAuth Returns Invalid credentials for Chimlkat.Imap


Trying to access a Google email account via IMAP with OAuth2 using the Chilkat IMAP email component. I am receiving this from the server response...

serverResponse: aaab NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)

We are using VB.NET, and I am following the example at https://www.example-code.com/csharp/gmail_imap_login_oauth2.asp. Connecting to imap.gmail.com using SSL and port 993. I am setting the Imap.AuthMethod = "XOAUTH2" and using the AuthToken instead of the password. The Imap.Login(LoginName, AuthToken) is where the error message is coming from.

I have enabled IMAP with the Gmail account, and I have enabled less secure apps. I confirmed the right auth token is getting set for the password, and I have checked the API scope is properly set to https://mail.google.com/. If I just try straight Login Name and Password the login is successful.

Not sure what else to check to figure out what is causing the Invalid credentials error.


Solution

  • The cause of this problem was found.

    In the call to Login, the application was passing "Bearer " instead of just "" for the password:

    Correct: success = imap.Login("[email protected]","");

    Incorrect: success = imap.Login("[email protected]","Bearer ");

    The access token is considered to be the password. Chilkat adds the required "Bearer" string internally when composes the XOAUTH2 data sent to the IMAP server.