Google Drive API Webhook - PullRequest
       112

Google Drive API Webhook

0 голосов
/ 29 мая 2020

Я настроил веб-перехватчик Google Диска через "свойство часов" (https://developers.google.com/drive/api/v2/reference/files/watch), он работает нормально и отправляет ответ, как только в файле просмотра обнаруживаются какие-либо изменения. Однако тело запроса (ieposted_data = request.get_data ()), как показано ниже, возвращается пустым (т.е. None). Я пробовал другие варианты, такие как запрос. json, но все еще пуст. Есть ли у кого-нибудь идеи о том, что я делаю неправильно? Мой код веб-перехватчика Python Flask приведен ниже и работает хорошо (т.е. публикуются все обновления файлов), за исключением того, что он возвращает пустой тип данных (ieposted_data = request.get_data () is 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)

1 Ответ

0 голосов
/ 29 мая 2020

Приведенный выше код работает, за исключением того, что 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)
...