python - Heroku 上的 Flask 应用程序无法检测到另一个文件进行身份验证
问题描述
我正在尝试为使用 Google Calendar API 设置约会的 Dialogflow 创建一个 webhook。因此 webhook 应该从 DialogFlow 获取 JSON 请求,放入 Google API 所需的格式并调用 Google API。我为此使用了服务器到服务器的身份验证。
我的问题是我不断收到错误 500(“webhook 调用失败”)。从错误日志看来,它找不到包含身份验证密钥的 JSON 文件。我不明白为什么,因为它位于包含 webhook 的同一个 Github 存储库中(它称为 g_calendar2.py)。插入到 python 文件中的路径是我在 Github 上打开 JSON 并单击“复制路径”时得到的路径。不过,当我在本地运行它时,身份验证很好。
有人知道吗?谢谢!
g_calendar2.py
from google.oauth2 import service_account
import googleapiclient.discovery
import datetime
from httplib2 import Http
from oauth2client import file, client, tools
from flask import Flask, request, abort, make_response
import json
import re
import os
app=Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_json(silent=True, force=True)
print(json.dumps(req, indent=4))
res = processRequest(req)
res = json.dumps(res, indent=4)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
def processRequest(req):
info=req.get("queryResult").get("parameters")
datetime=info.get("date-time")
data=datetime
def getEmails(str):
email_address=info.get("email")
str = str(email_address)
regex = r'([\w0-9._-]+@[\w0-9._-]+\.[\w0-9_-]+)'
return re.findall(regex, str, re.M|re.I)
emails=getEmails(str)
SCOPES = ['https://www.googleapis.com/auth/calendar']
SERVICE_ACCOUNT_FILE = '**Reponamedummy**/**filenamedummy**.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = googleapiclient.discovery.build('calendar', 'v3', credentials=credentials)
event = {
'start': {
'dateTime': data,
'timeZone': 'Asia/Kuala_Lumpur',
},
'end': {
'dateTime': '2018-09-06T14:10:14+08:00',
'timeZone': 'Asia/Kuala_Lumpur',
},
'attendees':
[{'email': str(emails[0])}]
}
response = service.events().insert(calendarId='ssar.developer@gmail.com', body=event).execute()
return 'Event has been created.'
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))
print("Starting app on port %d" % port)
app.run(debug=False, port=port, host='0.0.0.0')
错误日志
2018-08-31T08:20:19.704681+00:00 heroku[web.1]: State changed from starting to up
2018-08-31T08:21:04.742356+00:00 app[web.1]: {
2018-08-31T08:21:04.742402+00:00 app[web.1]: "responseId": "bd0ed761-466a-463a-b01f-41d664e4e5bf",
2018-08-31T08:21:04.742404+00:00 app[web.1]: "queryResult": {
2018-08-31T08:21:04.742408+00:00 app[web.1]: "queryText": "Slot for the 3rd of September, 3 p.m. samudrirao@gmail.com",
2018-08-31T08:21:04.742409+00:00 app[web.1]: "parameters": {
2018-08-31T08:21:04.742411+00:00 app[web.1]: "email": "samudrirao@gmail.com",
2018-08-31T08:21:04.742413+00:00 app[web.1]: "date": "",
2018-08-31T08:21:04.742414+00:00 app[web.1]: "date-time": "2018-09-03T12:00:00+08:00"
2018-08-31T08:21:04.742415+00:00 app[web.1]: },
2018-08-31T08:21:04.742417+00:00 app[web.1]: "allRequiredParamsPresent": true,
2018-08-31T08:21:04.742419+00:00 app[web.1]: "fulfillmentText": "Alright, you want to book for this date and time?",
2018-08-31T08:21:04.742420+00:00 app[web.1]: "fulfillmentMessages": [
2018-08-31T08:21:04.742421+00:00 app[web.1]: {
2018-08-31T08:21:04.742423+00:00 app[web.1]: "text": {
2018-08-31T08:21:04.742424+00:00 app[web.1]: "text": [
2018-08-31T08:21:04.742425+00:00 app[web.1]: "Alright, you want to book for this date and time?"
2018-08-31T08:21:04.742426+00:00 app[web.1]: ]
2018-08-31T08:21:04.742427+00:00 app[web.1]: }
2018-08-31T08:21:04.742429+00:00 app[web.1]: }
2018-08-31T08:21:04.742430+00:00 app[web.1]: ],
2018-08-31T08:21:04.742431+00:00 app[web.1]: "intent": {
2018-08-31T08:21:04.742437+00:00 app[web.1]: "name": "projects/linibot-9070f/agent/intents/5cb9cdd4-502c-4306-86a8-73e03e263d8f",
2018-08-31T08:21:04.742438+00:00 app[web.1]: "displayName": "Appointment Booking"
2018-08-31T08:21:04.742439+00:00 app[web.1]: },
2018-08-31T08:21:04.742441+00:00 app[web.1]: "intentDetectionConfidence": 1.0,
2018-08-31T08:21:04.742442+00:00 app[web.1]: "languageCode": "en"
2018-08-31T08:21:04.742443+00:00 app[web.1]: },
2018-08-31T08:21:04.742444+00:00 app[web.1]: "originalDetectIntentRequest": {
2018-08-31T08:21:04.742445+00:00 app[web.1]: "payload": {}
2018-08-31T08:21:04.742446+00:00 app[web.1]: },
2018-08-31T08:21:04.742448+00:00 app[web.1]: "session": "projects/linibot-9070f/agent/sessions/483fa518-f3ee-e3e3-805d-35187044d201"
2018-08-31T08:21:04.742449+00:00 app[web.1]: }
2018-08-31T08:21:04.744563+00:00 app[web.1]: [2018-08-31 08:21:04,742] ERROR in app: Exception on /webhook [POST]
2018-08-31T08:21:04.744566+00:00 app[web.1]: Traceback (most recent call last):
2018-08-31T08:21:04.744567+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
2018-08-31T08:21:04.744568+00:00 app[web.1]: response = self.full_dispatch_request()
2018-08-31T08:21:04.744570+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
2018-08-31T08:21:04.744571+00:00 app[web.1]: rv = self.handle_user_exception(e)
2018-08-31T08:21:04.744572+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
2018-08-31T08:21:04.744573+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2018-08-31T08:21:04.744575+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
2018-08-31T08:21:04.744576+00:00 app[web.1]: raise value
2018-08-31T08:21:04.744577+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
2018-08-31T08:21:04.744579+00:00 app[web.1]: rv = self.dispatch_request()
2018-08-31T08:21:04.744580+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
2018-08-31T08:21:04.744581+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2018-08-31T08:21:04.744582+00:00 app[web.1]: File "/app/g_calendar2.py", line 22, in webhook
2018-08-31T08:21:04.744584+00:00 app[web.1]: res = processRequest(req)
2018-08-31T08:21:04.744585+00:00 app[web.1]: File "/app/g_calendar2.py", line 57, in processRequest
2018-08-31T08:21:04.744586+00:00 app[web.1]: credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
2018-08-31T08:21:04.744590+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/google/oauth2/service_account.py", line 209, in from_service_account_file
2018-08-31T08:21:04.744591+00:00 app[web.1]: filename, require=['client_email', 'token_uri'])
2018-08-31T08:21:04.744597+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/google/auth/_service_account_info.py", line 71, in from_filename
2018-08-31T08:21:04.744599+00:00 app[web.1]: with io.open(filename, 'r', encoding='utf-8') as json_file:
2018-08-31T08:21:04.744605+00:00 app[web.1]: FileNotFoundError: [Errno 2] No such file or directory: '**Reponamedummy**/**filenamedummy**.json'
2018-08-31T08:21:04.745327+00:00 app[web.1]: 10.103.217.221 - - [31/Aug/2018:08:21:04 +0000] "POST /webhook HTTP/1.1" 500 291 "-" "Apache-HttpClient/4.5.4 (Java/1.8.0_181)"
2018-08-31T08:21:04.743647+00:00 heroku[router]: at=info method=POST path="/webhook" host=saloon-bot2.herokuapp.com request_id=676b00bc-5840-4c39-a5c2-b29ea62980bb fwd="35.193.216.215" dyno=web.1 connect=0ms service=4ms status=500 bytes=456 protocol=https
解决方案
推荐阅读
- c++ - how do I load a pure blackwhite image as qimage?
- wix - Wix - 如何在 MSI Uninstall 上卸载服务
- java - 如何通过 Generic arrayList 访问对象中的特定数据?
- python - 给定行值,在 Pandas Dataframe 中找到对应的行索引
- r - 如何创建一个循环来计算系统发育信号并将结果存储在 R 中的多个向量中
- sql - 在 SQL 中插入不需要的值
- jmeter - 检索到的令牌值未正确传递 - 反斜杠和管道更改为百分比和数字
- android - 为什么 ProgressBar 以编程方式不可见?
- django - 为什么 Django-summernote 中的下拉菜单不起作用?
- asp.net - 在页面加载上调用函数 LordRecord() 不会更新按钮单击 ASP.NET 上的数据