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

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

问题描述

我正在寻求帮助登录 gmail 帐户并使用 python 脚本下载一些电子邮件.我正在尝试采用 here,但我在第 1 步中遇到了问题,即通过 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.

这是我开始的代码:

import email
import imaplib

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

我收到以下错误:

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)'

确实在 gmail 设置中启用了 Imap.我已经查看了 google 支持链接上的说明以及在类似情况下有关此错误的问题,例如 here这里,但我的情况与第一个不同,因为
1) 它从一开始就没有用,并且
2) 我没有经常运行它而被阻止.它也与第二个示例不同,因为这是一个普通的 gmail 帐户,而不是带有 google 应用程序帐户的自定义域.
使用 https://accounts.google.com/DisplayUnlockCaptcha 尝试允许访问不会也不适合我.

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.

允许登录工作的唯一方法是将我的 google 帐户安全设置更改为允许访问不太安全的应用程序".

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

我的问题是:如何修复我的代码(或者更一般的设置)以允许我登录,而不放松我的帐户安全设置?有没有办法使用 libimap 来满足安全要求?

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?

推荐答案

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

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.

与此无关,您应该考虑在您的帐户上启用两步验证它更安全.如果这样做,您可以使用应用密码连接到 IMAP,这也可以避免以上警告.

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.

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

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