title: Webhook to track changes on Google Drive
body: <p>I am trying to create webhook to track a bunch of files on GDrive to get following information: who and when changed a file.
  So, using <a href="https://www.youtube.com/watch?v=HQLRPWi2SeA&amp;t=96s" rel="nofollow noreferrer">this</a> guide I created python server that listens for webhooks and exposed it via NGROK service.</p>
  <pre class="lang-py prettyprint-override"><code>from flask import json
  from flask import request
  import requests
  from flask import Flask, request, url_for, redirect, render_template
  
  app = Flask(__name__)
  
  @app.route('/')
  def api_root():
      return &quot;WEBHOOK&quot;
  
  @app.route('/*MyHtmlVerificationFile*')
  def admin():
      return render_template('*MyHtmlVerificationFile*')
  
  @app.route('/notif', methods=['POST'])
  def api_gd_message():
      data = request.get_data()
      header = request.headers
      print(data)
      print(header)
      return ''
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  <p>Then, according to <a href="https://medium.com/swlh/google-drive-push-notification-b62e2e2b3df4" rel="nofollow noreferrer">this</a> example I've verified ownership of the URL domain and registered it, got credential and wrote script to track changes.</p>
  <pre><code>from oauth2client.service_account import ServiceAccountCredentials
  from oauth2client.client import OAuth2Credentials
  from pygdrive3 import service
  import uuid
  import requests
  import json
  
  channel_id = str(uuid.uuid4())
  JSON_KEY_FILE = &quot;creds.json&quot;
  SCOPES = ['https://www.googleapis.com/auth/drive.file',
           'https://www.googleapis.com/auth/drive.readonly',
           'https://www.googleapis.com/auth/drive']
  credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, SCOPES)
  access_token_info = credentials.get_access_token()
  
  token = access_token_info.access_token
  
  header = {
      'Authorization': f'Bearer ' + token,
      'Content-Type': 'application/json',
  }
  
  body = {
      &quot;id&quot;: channel_id,
      &quot;type&quot;: &quot;web_hook&quot;,
      &quot;address&quot;: &quot;https://*MyDomain*/notif&quot;
  }
  
  r = requests.post(url='https://www.googleapis.com/drive/v3/changes/watch?pageToken=101',
                    data=json.dumps(body), headers=header)
  
  print(r.content)
  
  </code></pre>
  <p>Which result is:</p>
  <pre><code>b'{\n 
  &quot;kind&quot;: &quot;api#channel&quot;,\n 
  &quot;id&quot;: &quot;MyId&quot;,\n 
  &quot;resourceId&quot;: &quot;*MyResourceId*&quot;,\n 
  &quot;resourceUri&quot;: &quot;*MyRsourceUri*&quot;,\n 
  &quot;expiration&quot;: &quot;1612820606000&quot;\n
  }\n'
  
  Process finished with exit code 0
  </code></pre>
  <p>And there is server's message when Webhook launched.</p>
  <pre><code>127.0.0.1 - - [08/Feb/2021 21:43:20] &quot;POST /notif HTTP/1.1&quot; 200 -
  b''
  Host: *MyDomain*
  User-Agent: APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)
  Content-Length: 0
  Accept: */*
  Accept-Encoding: gzip,deflate,br
  X-Forwarded-For: 66.249.88.84
  X-Forwarded-Proto: https
  X-Goog-Channel-Expiration: Mon, 08 Feb 2021 21:43:26 GMT
  X-Goog-Channel-Id: *MyChannelId*
  X-Goog-Message-Number: 1
  X-Goog-Resource-Id: *MyResourcelId*
  X-Goog-Resource-State: sync
  X-Goog-Resource-Uri: *MyResourcelUri*
  
  </code></pre>
  <p>Problem is there are no messages when I do something (edit, delete, rename, etc.) with files on the Gdrive.</p>