当前位置: 首页 > 知识库问答 >
问题:

Google Drive API Webhook

段哲圣
2023-03-14
  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)

共有1个答案

申屠黎昕
2023-03-14

上面的代码可以工作,除了google将他们的响应作为标题发布(即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)
 类似资料:

相关问答

相关文章

相关阅读