使用python和imaplib登录gmail失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25413301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:11:28  来源:igfitidea点击:

gmail login failure using python and imaplib

pythonemailgmail-imap

提问by whopper510

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.

我正在寻求帮助登录 gmail 帐户并使用 python 脚本下载一些电子邮件。我正在尝试采用此处找到的方法,但在第 1 步中遇到了问题,即通过 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 hereand 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/DisplayUnlockCaptchato try and allow access doesn't work for me either.

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

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?

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

采纳答案by Jamie Nicolson

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 codethat shows the use of XOAUTH2 with imaplib.

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

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

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

回答by Alex Ols

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

你可以尝试开启这个:https: //www.google.com/settings/security/lesssecureapps这个动作为我解决了同样的问题。

回答by Raul Cavazos

With the new gmail′s update, some mail servr or apps get blocked due to the new gmails security settings. To solve this, I went to https://www.google.com/settings/securitypage and 'enabled' Access for less secure apps.

随着新 gmail 的更新,由于新的 gmail 安全设置,一些邮件服务器或应用程序被阻止。为了解决这个问题,我访问了https://www.google.com/settings/security页面并为不太安全的应用程序“启用”了访问权限。

回答by ?smund

I was able to solve this by using a text-mode browser (elinks) to verify my login from the remote server. I had already struggled a while, so I had already tried enabling unsafe apps and various other incantations.

我能够通过使用文本模式浏览器(elinks)从远程服务器验证我的登录来解决这个问题。我已经挣扎了一段时间,所以我已经尝试启用不安全的应用程序和其他各种咒语。

After logging in to gmail.com in elinks (using the html-only interface and having a security code sent to my phone) I could use imaplib to access the gmail account. Presumably one must do web-authentication from the same IP one tries to use Python/imaplib from.

在 elinks 中登录 gmail.com(使用纯 html 界面并将安全代码发送到我的手机)后,我可以使用 imaplib 访问 gmail 帐户。据推测,必须从尝试使用 Python/imaplib 的同一 IP 进行 Web 身份验证。

It's probably Better™ to use the OAuth protocol, but this way I got my script running again without rewriting it.

使用 OAuth 协议可能更好™,但这样我就可以再次运行我的脚本而无需重写它。