我已经设置了一个google drive的webhook通过 "手表属性"(https:/developers.google.comdriveapiv2referencefileswatch。),它工作得很好,一旦检测到观察文件有任何变化,就会提交响应。然而,请求主体(即posted_data=request.get_data( ))如下所示,返回的是空的(即None)。我试过其他选项,如request.json,但还是空的。有人知道我可能做错了什么吗?我的Python Flask webhook代码如下,除了返回一个空的数据类型(即posted_data=request.get_data( )是None)之外,工作得很好(即任何文件更新都会被发布)。任何建议都是非常感激的
from datetime import datetime
from flask import Flask, request, jsonify
import pytz
def get_timestamp():
dt=datetime.now(pytz.timezone('US/Central'))
return dt.strftime(("%Y-%m-%d %H:%M:%S"))
app = Flask(__name__)
@app.route('/webhook', methods=['POST','GET'])
def webhook():
if request.method=='GET':
return '<h1> This is a webhook listener!</h1>'
if request.method == 'POST':
posted_data=request.get_data( )
print("We have received a request =====>",posted_data)
cur_date=get_timestamp()
print("Date and time of update ====>",cur_date)
http_status=jsonify({'status':'success'}),200
else:
http_status='',400
return http_status
if __name__ == '__main__':
app.run(port=5000)
上面的代码工作,除了谷歌将发布他们的响应作为头(即request.headers)。请看下面的更新代码。
from datetime import datetime
from flask import Flask, request, jsonify
import pytz
def get_timestamp():
dt=datetime.now(pytz.timezone('US/Central'))
return dt.strftime(("%Y-%m-%d %H:%M:%S"))
app = Flask(__name__)
@app.route('/webhook', methods=['POST','GET'])
def webhook():
if request.method=='GET':
return '<h1> This is a webhook listener!</h1>'
if request.method == 'POST':
print(request.headers)
cur_date=get_timestamp()
print("Date and time of update ====>",cur_date)
http_status=jsonify({'status':'success'}),200
else:
http_status='',400
return http_status
if __name__ == '__main__':
app.run(port=5000)