【发布时间】:2020-05-28 22:35:30
【问题描述】:
我已经通过“watch property”(https://developers.google.com/drive/api/v2/reference/files/watch) 设置了一个 google drive webhook,它运行良好,并在检测到 watch 文件有任何更改时立即提交响应。但是,如下所示的请求正文(即posted_data=request.get_data( ))返回为空(即无)。我尝试了其他选项,例如 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)
【问题讨论】:
-
print(request.get_data()) 输出什么?
-
@Akib 它返回 None 类型...它是空的。
-
您使用的是 IDE 吗?你能检查一下你的请求对象中有什么吗?
-
@Akib 我将 webhook 托管在服务器上,但打印输出。谷歌不允许我在本地进行这些测试,因为它们必须发布在服务器 url 上并启用 ssl(即 https)。
-
我已经设法解决了这个问题。显然,谷歌通过标头发布响应(即 request.headers 而不是 request.get_data( ))。
标签: python-3.x flask google-api google-drive-api webhooks