Python 为什么SMTP会出现身份验证错误?

Python 为什么SMTP会出现身份验证错误?,python,google-api,google-sheets-api,Python,Google Api,Google Sheets Api,每次运行此程序时,它都会第一次运行,但第二次在终端中出现此错误: Traceback (most recent call last): File "main.py", line 30, in <module> timenow = datetime.now() File "main.py", line 27, in temperature moApparent = regexApparentTemp.search(str(json_object)) File

每次运行此程序时,它都会第一次运行,但第二次在终端中出现此错误:

Traceback (most recent call last):
  File "main.py", line 30, in <module>
    timenow = datetime.now()
  File "main.py", line 27, in temperature
    moApparent = regexApparentTemp.search(str(json_object))
  File "/usr/local/lib/python3.7/dist-packages/gspread/models.py", line 888, in append_row
    return self.spreadsheet.values_append(self.title, params, body)
  File "/usr/local/lib/python3.7/dist-packages/gspread/models.py", line 119, in values_append
    r = self.client.request('post', url, params=params, json=body)
  File "/usr/local/lib/python3.7/dist-packages/gspread/client.py", line 79, in request
    raise APIError(response)
gspread.exceptions.APIError: {
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}
我将autentication放在循环中的函数中,这样它就可以工作了,但它没有。有什么问题吗?

正如您在上看到的,已弃用

我建议你试试官方的:

def创建消息(发件人、收件人、主题、消息文本):
“”“为电子邮件创建消息。
Args:
发件人:发件人的电子邮件地址。
收件人:收件人的电子邮件地址。
主题:电子邮件的主题。
message_text:电子邮件的文本。
返回:
包含base64url编码的电子邮件对象的对象。
"""
message=MIMEText(message\u text)
消息['to']=to
邮件['from']=发件人
消息['subject']=主题
返回{'raw':base64.urlsafe\u b64encode(message.as\u字符串(
))}

如果您在上面的示例中遇到困难,请尝试

我在您的代码中没有看到SMTP(发送邮件传输协议)。如果出现错误,请首先使用
print()
检查变量中的内容。也许你发送了错误的值。顺便说一句:darksky以JSON的形式发送数据,这样你就不必转换成字符串并使用正则表达式,但是
expective=r.JSON()['current']['apparentTemperature']
temperature=r.JSON()['current']['temperature']
我为什么要放SMTP???不管怎样,它第一次工作,但是我得到了一个身份验证错误,也许你应该只进行一次授权-在
之前,而为True
。这不是答案。我不想发送电子邮件,我想做一个谷歌表单。这应该是作为建议的评论,而不是回答。问题是“为什么SMTP会出现身份验证错误?”。我不是故意说SMTP的,所以我道歉。但是,如果您阅读我代码的前三行,您会发现我根本没有使用SMTP。
import requests, pprint, re, gspread, time
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime



def temperature():
    from oauth2client.service_account import ServiceAccountCredentials
    from datetime import datetime
    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

    credentials = ServiceAccountCredentials.from_json_keyfile_name('Singapore-Weather-84def2be176a.json', scope)

    gc = gspread.authorize(credentials)

    wks = gc.open('Singapore Weather').sheet1
    r = requests.get('https://api.darksky.net/forecast/b02b5107a2c9c27deaa3bc1876bcee81/1.312914,%20103.780257')
    json_object = r.text


    regexCurrentTemp = re.compile(r'"temperature":(\d\d.\d\d)')
    moTemp = regexCurrentTemp.search(str(json_object))
    temperature = moTemp.group(1)

    regexApparentTemp = re.compile(r'"apparentTemperature":(\d\d.\d\d)')
    moApparent = regexApparentTemp.search(str(json_object))
    apparent = moApparent.group(1)

    timenow = datetime.now()
    wks.append_row([str(timenow), temperature, apparent])

while True:
    temperature()
    time.sleep(3597)