App password fails to connect to IMAP

This isn’t really a Python issue, but I’m not sure where to ask. The various Google communities I’ve consulted have all been useless.

I have a Python app which used to use an app password to connect to Gmail via imaplib. This program runs in the background, but just today I noticed that it can no longer connect:

2024-12-24 15:21:01,571 DEBUG > b'XXXXX LOGIN **REDACTED**
2024-12-24 15:21:02,602 DEBUG < b'XXXXX NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)'
...
2024-12-24 15:21:02,604 ERROR login failed. check your credentials.
2024-12-24 15:21:02,604 ERROR Exiting read loop early

The ERROR messages are coming from my program, the DEBUG messages from the IMAP chit chat. Looking through my logs, this started happening in early November. Before that, it had been working fine for over two years.

I couldn’t figure out why my app password was no longer working, though in poking around my Google account, I suspect it has something to do with enabling passkeys. I can’t find two-factor authentication anywhere in my account. Is there some other way for background applications (no GUI) to use IMAP to connect to GMail?

Do you need to use oath?
I found this python - use imaplib and oauth for connection with Gmail - Stack Overflow

Or maybe you need to use a app token as this hints at Accessing Gmail with Python: A Beginner’s Guide | by Masego | Medium

Okay, I figured this out. Google instituted something recently called “Advanced Protection.” Enabling this apparently disables app passwords altogether. After I turned that off, I was able to create a new app password and tweak my app’s config file.

1 Like

I think OAuth is the correct solution for interactive applications. I’m not sure that would work for an application with no user interface. (My app runs using cron.)

Nothing about OAuth requires an interative app. It’s just an API.
If it works when you have the “Advanced Protection” enabled it might be useful.

Oh if you have 2FA then that the interactive bit I guess!