无法使用gspread进行身份验证以访问Google云端硬盘

时间:2015-06-01 11:32:01

标签: python gmail gmail-api gspread

我使用python创建了一种方法来跟进我已经发送过书籍的潜在客户,但却没有收到回复的信息。快速赶上他们的方法。必须强调这不是用于垃圾邮件。

我有一个文件gmail_variable.py,其中包含GMAIL_USERNAME =" myemail@gmail.com" GMAIL_PASSWORD =" myGmailPassword"

以下内容使用这些详细信息登录Google云端硬盘,并向尚无法回复我的任何人发送电子邮件。工作到今天都很好。开始收到以下错误。我从终端运行这个并将代码本地保存在我的机器上。

Traceback (most recent call last):
File "pr_email_robot.py", line 6, in <module>
gc = gspread.login(GMAIL_USERNAME, GMAIL_PASSWORD)
File "/Library/Python/2.7/site-packages/gspread/client.py", line 312, in login
client.login()
File "/Library/Python/2.7/site-packages/gspread/client.py", line 119, in login
"Unable to authenticate. %s code" % ex.code)
gspread.exceptions.AuthenticationError: Unable to authenticate. 404 code

以下是执行的代码。根据我读到的内容,我知道oAuth是4月20日的变化。但是下面的代码一直在努力。

经过一些初步研究后,我发现gspread建议更换

gc = gspread.login(GMAIL_USERNAME, GMAIL_PASSWORD)

gc = gspread.authorize(OAuth2Credentials)

然后我浏览了指南here并按照他们的建议设置了API。下载了JSON文件。我应该用OA替换OAuth2Credentials?

gc = gspread.authorize(OAuth2Credentials)

任何想法或建议都非常感谢。对python来说仍然很新,所以简单的解释是有帮助的:)

import smtplib
import gspread

from gmail_variables import *

gc = gspread.login(GMAIL_USERNAME, GMAIL_PASSWORD)
wks = gc.open("horror_reviewers").sheet1


recipients = wks.get_all_values()

def sendEmail(recipient):
    email_subject = "Jay-Jay"
    #recipient = "nat@programmingformarketers.com"

    body_of_email = "<body><p>Hello "+ recipient[1].encode('utf-8')  +",<br /> \
                    <br /> \
                    We spoke in the last few months X book.<br /> \
                    <br /> \
                    Have a note that we sent you a copy and confirmed a review. Did we send over what you needed or are you still awaiting details from us?</a><br /> \
                    <br /> \
                    Apologies if you have already posted the link and we missed it. If you could resend that would be great.<br /> \
                    <br /> \
                    </p></body>"

    session = smtplib.SMTP('smtp.gmail.com', 587)
    session.ehlo()
    session.starttls()
    session.login(GMAIL_USERNAME, GMAIL_PASSWORD)

    headers = "\r\n".join(["from: " + GMAIL_USERNAME,
                           "subject: " + email_subject,
                           "to: " + recipient[0],
                           "mime-version: 1.0",
                           "content-type: text/html"])

    # body_of_email can be plaintext or html!                    
    content = headers + "\r\n\r\n" + body_of_email
    session.sendmail(GMAIL_USERNAME, recipient[0], content)

[sendEmail(i) for i in recipients]

1 个答案:

答案 0 :(得分:0)

您可以使用SMTP使用Gmail发送邮件:

代码示例:

import smtplib
from email.mime.text import MIMEText

hostname = "smtp.gmail.com"
password = "<password>"
me = "<me@gmail.com>"
you = "<you@gmail.com>"

payload = "some text here"
msg = MIMEText(payload)

msg["Subject"] = "Test subject"
msg["From"] = me
msg["To"] = you

session = smtplib.SMTP_SSL(hostname)
session.login(me, password)
session.sendmail(me, [you], msg.as_string())
session.quit()

来源:

从这里接受了这个想法:http://vi3k6i5.blogspot.in/2015/03/how-to-extract-email-ids-from-your.html

以及此处的代码:http://www.reddit.com/r/Python/comments/15n6dw/sending_emails_through_python_and_gmail/