【问题标题】:Google Drive API WebhookGoogle Drive API 网络钩子
【发布时间】: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


【解决方案1】:

除了谷歌将他们的响应发布为标题(即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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多