gmail登录失败使用python和imaplib [英] gmail login failure using python and imaplib

查看:1262
本文介绍了gmail登录失败使用python和imaplib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求一些帮助登录Gmail帐户,并使用python脚本下载一些电子邮件。我正在尝试调整一种方法:这里,但是我遇到第1步的问题,通过imap访问帐户。



这是我开始的代码:

  import电子邮件
import imaplib

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

我收到以下错误:

 追溯(最近的最后一次呼叫):
文件email.py,第1行,< module>
导入电子邮件,imaplib
文件/home/will/wd/email.py,第14行在< module>
m.login('myemailaddress','mypassword')
文件/usr/lib/python3.4/imaplib.py,第538行,登录
raise self.error( dat [-1])$ ​​b $ b imaplib.error:b'[ALERT]请通过网络浏览器登录:http://support.google.com/mail/accounts/bin/answer.py?answer=78754 (失败)'

确实在gmail设置中启用了Imap。我已经在类似的情况下查看了关于Google支持链接的说明和有关此错误的问题,例如这里这里,但是我的情况与第一个不同,因为
1)它从来没有开始,而
2)我没有如此频繁地运行以便被阻止。它也不同于第二个例子,因为这是一个普通的Gmail帐户,而不是具有Google Apps帐户的自定义域。
使用 https://accounts.google.com/DisplayUnlockCaptcha 尝试并允许访问



允许登录工作的唯一方法是将我的Google帐户安全设置更改为允许访问较不安全的应用。我的问题是:如何修复我的代码(或我的设置更一般),以允许我登录,而不放松我的帐户安全设置?有没有办法使用libimap来满足安全要求?

解决方案

如果您想避免此错误而不影响帐户的安全性,请使用OAuth进行身份验证。该协议记录在此处,而 Python示例代码,显示了使用XOAUTH2与imaplib。



除此之外,您应考虑在您的帐户中启用两步验证使其更安全。如果这样做,您可以使用应用密码连接到IMAP,这也可能会避免上述警告。


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?

解决方案

If you want to avoid this error without compromising your account's security, use OAuth to authenticate. The protocol is documented here, and there is Python sample code that shows the use of XOAUTH2 with imaplib.

Independent of this, you should consider enabling two-step verification on your account to make it more secure. If you do, you can use an App Password to connect to IMAP, which might also avoid the above warning.

这篇关于gmail登录失败使用python和imaplib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆