python - How to return a modified google doc's id from the webhook notification? - Stack Overflow

currently I have a webhook watching a Google Drive folder, and I have it set up to tell me when somethi

currently I have a webhook watching a Google Drive folder, and I have it set up to tell me when something changed in the folder (document modified / created / deleted). I want to get the id of the document that changed so I can reference it in my database table and update it (or create/delete if the response is about it being created or deleted). Does anyone know how to get the ID from a webhook / Google notification response? (in python too would be helpful)

drive_webhook.py:

from googleapiclient.discovery import build
from google.oauth2.service_account import Credentials
import requests
SERVICE_ACCOUNT_FILE = 'censored.json'
SCOPES = ['.readonly', '.readonly', '', '']
def subscribe_to_folder_changes(folder_id, webhook_url):
    creds = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    drive_service = build('drive', 'v3', credentials=creds)
    # Create a unique ID for your webhook channel
    channel_id = "727a5eaf-0b0d-426a-b7ba-fb59dedda7e8"  # You can generate a unique identifier for this
    # Set up the webhook channel request
    request_body = {
        "id": channel_id,
        "type": "web_hook",
        "address": webhook_url,
        "params": {
            "ttl": "2592000"  # Channel expiration (optional, 30 days in seconds), we need to look into auto-renewing subscription
        }
    }
    # Subscribe to changes for the specified folder
    response = drive_service.files().watch(fileId=folder_id, body=request_body).execute()
    print("Webhook subscription created:", response)
if __name__ == "__main__":
    folder_id = 'censored' 
    # ngrok sessions expire after several hours on the free plan. 
    # If you restart ngrok, it will generate a new URL, so you’ll need to update your webhook subscription with the new URL.
    # When moving to production, you'll want a consistent public URL, provided by a hosting service (e.g., AWS, Heroku).
    webhook_url = '' 
    subscribe_to_folder_changes(folder_id, webhook_url)

notification_handler.py:

from flask import Flask, request
from doc_retrieval import retrieve_document_ids
from google.oauth2.service_account import Credentials
app = Flask(__name__)
# Set your Google credentials file and folder ID
SERVICE_ACCOUNT_FILE = 'censored.json'
folder_id = 'censored'
@app.route('/notifications', methods=['POST'])
def notifications():
    # Check if it's a valid notification
    resource_state = request.headers.get('X-Goog-Resource-State')
    # If the resource state indicates a folder update, call the document retrieval functions
    if resource_state == 'update':
        print("Change detected in Google Drive folder.")
        creds = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=['.metadata.readonly'])
        retrieve_document_id( # whatever parameter ) # ideal function i want to get
    return '', 200  # Return 200 OK to acknowledge receipt
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742324927a4422555.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信