Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
921 views
in Technique[技术] by (71.8m points)

email - gmail login failure using python and imaplib

I'm seeking some help logging into a gmail account and downloading some emails using a python script. I'm trying to adapt an approach found here, but I'm running into a problem with step 1, accessing the account via imap.

here is the code I'm starting with:

import email
import imaplib

m = imaplib.IMAP4_SSL("imap.gmail.com",993)
rc, resp = m.login('myemailaddress','mypassword')

I get the following error:

Traceback (most recent call last):
  File "email.py", line 1, in <module>
    import email, imaplib
  File "/home/will/wd/email.py", line 14, in <module>
     m.login('myemailaddress','mypassword')
  File "/usr/lib/python3.4/imaplib.py", line 538, in login
    raise self.error(dat[-1])
 imaplib.error: b'[ALERT] Please log in via your web browser: http://support.google.com/mail/accounts/bin/answer.py?answer=78754 (Failure)'

Imap is indeed enabled in gmail settings. I have looked at the instruction on the google support link and questions regarding this error in similar situations, such as here and here, but my situation is different from the first because
1) it never worked to start with, and
2) I'm not running it so frequently to get blocked. Its also different from the second example because this is a normal gmail account, not a custom domain with a google apps account.
Using the https://accounts.google.com/DisplayUnlockCaptcha to try and allow access doesn't work for me either.

The only thing that allows login to work is to change my google accounts security settings to "allow access for less secure apps".

My question is: how can I fix my code (or my setup more generally) to allow me to login, without relaxing my accounts security settings? Is there some way to meet security requirements using libimap?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try to turn on this: https://www.google.com/settings/security/lesssecureapps This action solved the same problem for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...