简体   繁体   中英

Unable to authenticate using gspread to access Google Drive

I use python to generate a way to follow up with prospects who I have sent books to but haven't heard back from. Quick way to catch up with them. Must stress this isn't used for SPAM.

I have one file gmail_variable.py which contains GMAIL_USERNAME = "myemail@gmail.com" GMAIL_PASSWORD = "myGmailPassword"

The below uses those details to login to Google Drive and email anyone who hasn't yet got back to me. Working absolutely fine until today. Started receiving the following error. I run this from my terminal and keep the code locally on my machine.

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

Below is the code that gets executed. I know oAuth was changes on April 20th according to something I read. But the code below was working up until then.

After some initial research I discovered that gspread recommend replacing

gc = gspread.login(GMAIL_USERNAME, GMAIL_PASSWORD)

with

gc = gspread.authorize(OAuth2Credentials)

I then went through the guide here and setup the API as they suggested. Downloaded the JSON file. BUT what do I replace OAuth2Credentials with?

gc = gspread.authorize(OAuth2Credentials)

Any ideas or advice greatly appreciated. Still very new to python so simple explanations are helpful :)

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]

You can use SMTP to send mail using gmail:

code example:

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()

Source:

Took the idea from here: http://vi3k6i5.blogspot.in/2015/03/how-to-extract-email-ids-from-your.html

and code from here: http://www.reddit.com/r/Python/comments/15n6dw/sending_emails_through_python_and_gmail/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM