Smartsheet Webhook Payload Construction Error

25 views Asked by At

I'm building python scripts to automate registering a series of webhooks to listen to various sheets. the script is pulling sheet ids from a json dictionary and looping through them to register each one. Based on the error it appears there is something incorrect or missing in the webhook payload. Appreciate any help I can get troubleshooting this error:

Registering webhook: Atlanta, GA - Deal Data_Webhook for sheetId 7673571595052932 Making request to https://api.smartsheet.com/2.0/webhooks with data: None Making request to https://api.smartsheet.com/2.0/webhooks with data: {'name': 'Atlanta, GA - Deal Data_Webhook', 'callbackUrl': 'http://34.130.93.39:5000/webhook_listener', 'scope': 'sheet', 'scopeObjectId': 7673571595052932, 'events': ['sheet.created', 'sheet.updated', 'row.created', 'row.updated'], 'enabled': True} HTTPError: 400 - { "errorCode" : 1008, "message" : "Unable to parse request. The following error occurred: Field "null" was of unexpected type.", "refId" : "oghfzq"

Script is below. Can also provide the api_request_lib.py script if needed.

from flask import Flask, request, jsonify
import json
from api_request_lib import SmartsheetClient

app = Flask(__name__)
api_key = "api key here"
client = SmartsheetClient(api_key)

def webhook_exists(client, webhook_name):
    webhooks = client.list_webhooks().get('data', [])
    for webhook in webhooks:
        if webhook['name'] == webhook_name:
            return True, webhook
    return False, None

def register_sheet_webhooks(client, sheetId_dict, callback_url, events):
    for sheet_name, sheet_id in sheetId_dict.items():
        # Debug - print out the sheet_id and webhook_name to check they're correct
        print(f"Registering webhook: {sheet_name}_Webhook for sheetId {sheet_id}")
        webhook_name = f"{sheet_name}_Webhook"
        does_exist, _ = webhook_exists(client, webhook_name)
        if not does_exist:
            # Pass sheet_id as the scope_object_id argument to the register_webhook method
            response = client.register_webhook(webhook_name, callback_url, 'sheet', sheet_id, events)
            # Check for response errors
            if response and 'errorCode' in response:
                print(f"Error registering webhook for {sheet_name}: {response}")

@app.route('/webhook_listener', methods=['POST'])
def webhook_listener():
    data = request.json
    print('Webhook received data:', data)
    # Process the received data, based on your needs
    return jsonify(success=True)

if __name__ == '__main__':
    # Load sheetId_dict from sheetId_dict.json
    try:
        with open('sheetId_dict.json', 'r') as f:
            sheetId_dict = json.load(f)
    except FileNotFoundError:
        print("Error: sheetId_dict.json not found. Make sure it exists in the same directory as this script.")
        sheetId_dict = {}

    CALLBACK_URL = 'http://0.0.0.0:5000/webhook_listener'  # Replace with the externally accessible URL to your Flask app
    EVENTS = ["sheet.create", "sheet.update", "row.create", "row.update"]  # Specify the type of events the webhook should listen for

    # Register a webhook for each sheet in sheetId_dict
    register_sheet_webhooks(client, sheetId_dict, CALLBACK_URL, EVENTS)

    app.run(host='0.0.0.0', port=5000)  # Starts the Flask application server

The script runs and loops properly, but each loops throws the error HTTPError: 400 - { "errorCode" : 1008, "message" : "Unable to parse request. The following error occurred: Field "null" was of unexpected type.", "refId" : "oghfzq"

0

There are 0 answers