You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

graestar888's avatar
graestar888
Explorer | Level 4
5 months ago

Webhook configuration

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

  • Здравко's avatar
    Здравко
    Legendary | Level 20

    graestar888 wrote:

    ...
    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.
    ...


    Hi graestar888,

    Let's see what's there. A good way to debug something is to reproduce the issue. Since you work on local machine and cannot rely on original request you have to reproduce it as correct as possible. Right? 😉 And so what are you using:


    graestar888 wrote:

    ...

    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/
    ...


    Hm..🤔 How do you conclude that this curl command reproduces the webhook in correct manner? Did you record some of the requests and then reproduce it or...??? 🙂 Rhetoric of course. Never suppose but check it (something you have skipped that lead you to wrong direction)!!! 😉 For more info about request body that you may expect, take a look here (again, aside of that you can just record one such request and reproduce it with curl). There are other mistakes too, but I believe once you fix you start point you will see and fix everything else.

    Hope this helps.

     

    PS: The way you're using is synchronous processing of request - something that's discouraged since the response has to be fast (otherwise Dropbox will detect it as a fail and may stop further notifications)! Execute the actual processing (your process_webhook) in detached thread (or in similar way).

    • graestar888's avatar
      graestar888
      Explorer | Level 4

      Thank you! @Здравко for getting back to me.

      Please let me ponder that and I will try to work out how to approach this. I am new to this, so it will likely take me a bit of time. Also, I really appreciate the tip on synchronous processing, I wasn't aware about that.

      • iNeil's avatar
        iNeil
        Icon for Dropbox Engineer rankDropbox Engineer

        Hi graestar888 ,

        In addition to what Здравко mentioned, please bear in mind that the webhook notifications only indicate when something has changed in an account, but do not themselves indicate what has changed.

        To retrieve information on the file changes, you can use the files functionality on the Dropbox API itself to retrieve the necessary information. I recommend reading the following guides for more information:

        https://developers.dropbox.com/detecting-changes-guide
        - https://developers.dropbox.com/dbx-file-access-guide

         

        Please feel free to let me know if you have any questions, or if there is anything else I can help you with. 

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,910 PostsLatest Activity: 3 days ago
333 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!