开发者

Using imaplib to log into Gmail using credentials from a file

开发者 https://www.devze.com 2023-03-19 18:52 出处:网络
I apologise in advance, I know this will be a simple answer but I\'m not having any luck and it\'s starting to bug me.

I apologise in advance, I know this will be a simple answer but I'm not having any luck and it's starting to bug me.

I just want to log into my Gmail account using imaplib and credentials pulled from a file. I can log into Gmail no worries using:

srv = imaplib.IMAP_SSL("imap.gmail.com")
srv.login("bob@gmail.com", "mypassword")

But instead I'm trying to use a text file (credentials.dat). Inside the file is 2 lines:

bob@gmail.com
mypassword

My code is as follows:

import imaplib
CREDS = open("credentials.dat", "r")

account = CREDS.readline()
password = CREDS.readline()

srv = imaplib.IMAP4_SSL("imap.gmail.com")
srv.login(account, password)

When I run it I get the followign error:

Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/usr/lib64/python2.4/imaplib.py", line 480, in login
       typ, dat = self._simple_command('LOGIN', user, self._quote(password))
    File "/usr/lib64/python2.4/imaplib.py", line 1028, in _simple_command
       return self._command_complete(name, self._command(name, *args))
    File "/usr/lib64/python2.4/imaplib.py", line 865, in _command_complete
       raise self.error('%s command error: %s %s' % (name, typ, data))
       imaplib.error: LOGIN command error: BAD ['Failed to parse your command      r13if7033890ebd.47']

I assume it doesn't like the data type being entered into srv.login, but I have no idea how else to do it.

There are so many more things I want to do onc开发者_如何学JAVAe I get into Gmail (read emails, post, etc), but it's looking like an uphill battle if I can't even log in :(

Thanks for your time.


readline() includes the newline character at the end of the line, so you're really saying: login with 'bob@gmail.com\n'

Try readline().strip() to strip whitespace characters from both sides of the line.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号