0

I want to connect to gmail using imapclient. I've followed the example from the book "Automate the Boring Stuff with Python" chapter 16 by Al Sweigart. The code from the book works well in interactive mode. However, Al and others warn to not include passwords in a program that you run. I did set up an app password, which works like a charm in interactive mode. But, an error returns when I try to send the password from the keyboard as standard input while running it as a script. I've also tried input(), which also doesn't work. I have avoided putting the app password directly into the code; would that be an acceptable practice by most standards? Also, I've tried the code below from the linux terminal, and from the spyder development environment, both ways return errors.

import imapclient

import getpass

imapObj = imapclient.IMAPClient('imap.gmail.com', ssl=True)

app_password = getpass.getpass('Password')

imapObj.login('john@gmail.com', 'app_password')

expected results: successful login

Actual results:

File "/home/john/anaconda3/lib/python3.6/site-packages/imapclient/imapclient.py", line 345, in login
raise exceptions.LoginError(str(e))

LoginError: b'[AUTHENTICATIONFAILED] Invalid credentials (Failure)'
Omer Tekbiyik
  • 4,255
  • 1
  • 15
  • 27
John
  • 11
  • 6

0 Answers0