Python 无法使用gspread访问Google Drive进行身份验证
我使用python生成一种方法来跟踪那些我曾向其发送过书籍但尚未收到回复的潜在客户。快速赶上他们的方法。必须强调这不是用于垃圾邮件 我有一个文件gmail_variable.py,其中包含gmail_用户名=”myemail@gmail.com“GMAIL_PASSWORD=“MyGmail密码” 下面使用这些详细信息登录到Google Drive,并向尚未回复我的任何人发送电子邮件。工作到今天都很好。开始接收以下错误。我从终端运行这个程序,并将代码保存在本地计算机上Python 无法使用gspread访问Google Drive进行身份验证,python,gmail,gmail-api,gspread,Python,Gmail,Gmail Api,Gspread,我使用python生成一种方法来跟踪那些我曾向其发送过书籍但尚未收到回复的潜在客户。快速赶上他们的方法。必须强调这不是用于垃圾邮件 我有一个文件gmail_variable.py,其中包含gmail_用户名=”myemail@gmail.com“GMAIL_PASSWORD=“MyGmail密码” 下面使用这些详细信息登录到Google Drive,并向尚未回复我的任何人发送电子邮件。工作到今天都很好。开始接收以下错误。我从终端运行这个程序,并将代码保存在本地计算机上 Traceback (mo
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
与
然后我阅读了指南并按照他们的建议设置了API。下载了JSON文件。但是我应该用什么来替换OAuth2Credentials呢
gc = gspread.authorize(OAuth2Credentials)
非常感谢您的任何想法或建议。对python来说还是很新的,所以简单的解释很有帮助:)
导入smtplib
进口gspread
从gmail_变量导入*
gc=gspread.login(GMAIL\u用户名、GMAIL\u密码)
wks=gc.open(“恐怖审查人”)。表1
收件人=周。获取所有值()
def发送电子邮件(收件人):
电子邮件\u subject=“Jay Jay”
#收件人=”nat@programmingformarketers.com"
电子邮件正文=“你好”+收件人[1]。编码('utf-8')+,
\
\
我们在过去的几个月里谈过X书。
\
\
请注意,我们已向您发送了一份副本并确认了审查。我们是否已发送了您需要的内容,或者您是否仍在等待我们的详细信息?
\
\
如果您已经发布了链接,但我们错过了链接,请道歉。如果您可以重新发送,那将非常好。
\
\
”
session=smtplib.SMTP('SMTP.gmail.com',587)
session.ehlo()
session.starttls()
会话.登录(GMAIL\u用户名,GMAIL\u密码)
headers=“\r\n”。加入([”from:“+GMAIL\u用户名,
“主题:”+电子邮件主题,
收件人:“+收件人[0],
“mime版本:1.0”,
“内容类型:text/html”])
#电子邮件的正文可以是纯文本或html!
内容=邮件头+“\r\n\r\n”+邮件正文
session.sendmail(GMAIL_用户名,收件人[0],内容)
[为收件人中的i发送电子邮件(i)]
您可以使用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()
导入smtplib
从email.mime.text导入MIMEText
hostname=“smtp.gmail.com”
password=“”
me=“”
you=“”
payload=“此处有一些文本”
msg=MIMEText(有效负载)
msg[“主题”]=“测试主题”
msg[“From”]=我
msg[“To”]=您
session=smtplib.SMTP\u SSL(主机名)
session.login(我,密码)
session.sendmail(我,[you],msg.as_string())
session.quit()
资料来源:
从这里开始:
这里的代码是:谢谢@VikashSingh,但它与Google Drive的集成方式与现有系统不同。你能试试这个链接,看看它是否适用于u:Thank you@VikashSingh。是的,我一路读了那篇文章,找到了“你会自动下载一个包含这些数据的JSON文件”这一部分,这就是我被卡住的地方。这就是我正在努力解决的代码<代码>从oauth2client.client导入SignedJwtAssertionCredentials json_key=json.load(打开('gspread-april-2cd…ba4.json'))作用域=['https://spreadsheets.google.com/feeds“]credentials=SignedJwtAssertionCredentials(json_-key['client_-email'],json_-key['private_-key'],作用域)gc=gspread.authorize(凭证)
gc = gspread.authorize(OAuth2Credentials)
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]
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()