问题
How can I connect to imaplib library without encountering AUTHENTICATIONFAILE error !?
My Gmail inbox shows me a serious security alert (login attempt blocked) when logging in through the web browser.
IMAP_SERVER = 'imap.gmail.com'
USERNAME = '******@gmail.com'
PASSWORD = '******'
client = imaplib.IMAP4_SSL(IMAP_SERVER)
client.login(USERNAME, PASSWORD)
回答1:
This is most likely gmail blocking "less secure apps" from connecting to your account. This blocking is enabled, by default.
From a google support page:
You can block sign-in attempts from some apps or devices that are less secure. Apps that are less secure don't use modern security standards, such as OAuth. Using apps and devices that don’t use modern security standards increases the risk of accounts being compromised. Blocking these apps and devices helps keep your users and data safe.
If you would like to turn on access to less secure apps (which will make your account more vulnerable to being attacked), then you can follow these steps:
Sign into your google account normally. (https://myaccount.google.com/)
Once signed in, go to the "Security" page.
Scroll down until you see a window with "Less secure app access". If you want to access your email using python code like you posted, this needs to be turned on.
Personally, I think this is acceptable on a throwaway/testing account but I would never do this to my main/personal account or any account involving communicating with clients.
In fact, take a look at this page: https://developers.google.com/gmail/api/quickstart/python Gmail has a dedicated API for connecting to your google mail account. Their api implements proper security measures which means you won't need to enable less secure app access... protecting your account!
来源:https://stackoverflow.com/questions/57963552/i-encounter-an-authenticationfailed-error-when-connecting-to-email-with-imaplib