Hi everyone, new to this forum!
I'm new to Dropbox development (so apologies for this basic query) and implementing webhooks into my app to download files when a state change occurs (new file added) using the list_folder endpoint. My code is as follows:
I work with django and Python and even though I have added debugging, the log files on this occasion are not very insightful, in that I only get a 200 response, which doesn't tell me much apart from that there was a successful post request - after a file was added to the source folder. That's where it stops and the file download does not follow. I should point out I am using Local Tunnel to test in my local environment as I am not yet ready to go to a staging server, so perhaps this is the reason where things are falling over? The Local Tunnel url I am given does enable when configuring the endpoint url in the webhook URI on the Dropbox developer console though.
My code is as follows:
@method_decorator(csrf_exempt, name='dispatch')
class DropboxWebhookView(View):
def get(self, request, *args, **kwargs):
# Handle GET requests from Dropbox for verification
challenge = request.GET.get('challenge')
if challenge:
logger.info(f'Challenge received: {challenge}')
return HttpResponse(challenge)
logger.error('Challenge not found in request')
return HttpResponse(status=400)
def post(self, request, *args, **kwargs):
# Handle POST requests from Dropbox to process webhook events
payload = request.body
# Process the payload (e.g., check for new file events)
# Trigger file processing workflow
logger.debug(payload)
process_webhook(payload)
return JsonResponse({'status': 'received'})
def process_webhook(payload):
logger.debug(payload)
logger.info(payload)
data = json.loads(payload)
changes = data.get('changes', [])
for change in changes:
if change.get('is_directory', False):
continue
file_path = change.get('path_lower')
# Check if the file is in the specific folder being monitored
if file_path.startswith('/expenses/'):
handle_new_file(file_path)
def handle_new_file(file_path):
try:
metadata, response = dbx.files_download(path=file_path)
# Define the path to your destination folder
destination_path = f'./media/dropbox/{metadata.name}'
with open(destination_path, 'wb') as f:
f.write(response.content)
except dropbox.exceptions.ApiError as e:
logger.error(f'Error handling new file: {e}')
The log files output the following:
INFO "POST /api/webhook/dropbox/ HTTP/1.1" 200 22
I have tested this with a curl command as follows and that works. So am struggling a bit debugging this, as I don't know where else to look for further insight into why this isn't working.
This is my curl command and this calls the same class and functions and does work as expected i.e. downloads a file when a file is added to the source folder:
curl -X POST -H "Content-Type: application/json" -d '{"changes":[{"path_lower":"/expenses/lunch-receipt.jpg","is_directory":false}]}' http://localhost:8001/api/webhook/dropbox/
I'd be really happy to hear from anyone who has experienced the same or might know of some ways I can try and establish where I am going wrong.
Appreciate your help!
Best,
Graeme