I kept getting “Invalid Credentials” in Thunderbird when connected to my Gmail IMAP account. I found the solution to be:

https://www.google.com/accounts/UnlockCaptcha?