3

I am trying to login to gmail and read the messages from python package. But getting an Error : [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)

import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("username@gmail.com","password")
m.select('"[Gmail]/All Mail"')

result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
    for num in data[0].split():
        result, data = m.uid('fetch', num, '(RFC822)')
    if result == 'OK':
        email_message = email.message_from_bytes(data[0][1])    # raw email text including headers
        print('From:' + email_message['From'])

m.close()
m.logout()
Diesel Kumar
  • 215
  • 4
  • 11
  • 22
  • https://stackoverflow.com/questions/25413301/gmail-login-failure-using-python-and-imaplib – AnFi Aug 25 '17 at 17:59
  • Possible duplicate of [gmail login failure using python and imaplib](https://stackoverflow.com/questions/25413301/gmail-login-failure-using-python-and-imaplib) – AnFi Aug 25 '17 at 18:00
  • @AndrzejA.Filip : How to use imaplib, without two-step verification ? I don't want to use two-step verification. is there any simple script which shows usage of only with credentials or with Oauth2 ? – Diesel Kumar Aug 25 '17 at 18:17
  • 1
    The link is available at the page mentioned in the error messge: "Allowing less secure apps to access your account" => https://support.google.com/accounts/answer/6010255 . It should make gmail IMAP behave like classic IMAP. – AnFi Aug 25 '17 at 18:29
  • @AndrzejA.Filip Got the second way to make it less secure. If at all to use credentials, how do i do that ? – Diesel Kumar Aug 25 '17 at 18:37
  • @AndrzejA.Filip : Without setting to low security settings, any way to deal with code ? – Diesel Kumar Aug 26 '17 at 05:47
  • @AndrzejA.Filip : I've enabled "Less Secure for apps". But even then, getting the same error. – Diesel Kumar Aug 26 '17 at 05:58
  • Log once - AFAIR google/gmail may want you to clean "suspicious activity detected" flags. – AnFi Aug 26 '17 at 06:26
  • @AndrzejA.Filip resolved it, some settings issue. any code to deal without changing to low security settings ? – Diesel Kumar Aug 26 '17 at 06:41

2 Answers2

7

It's work for me:

CK.Nguyen
  • 1,258
  • 17
  • 16
  • It's better if you switch to you App password, see more https://support.google.com/accounts/answer/185833?hl=en – CK.Nguyen Oct 01 '20 at 09:15
1

Just turn this ON for your Gmail account - https://myaccount.google.com/lesssecureapps