Im currently working on a application were i need to implement webhooks from my dropbox to my local pc. Unfortunatly im having problems with getting my program to work with the webhooks.
My process so far has been roughly as follows.
- Ive setup a app on the dorpbox website and given it most of the permissions.
- Ive generated a webhook using webhook.site and put a $request.query.challenge$ in the body. Ive added it to the
dropbox webhook list and it enables it. When I delete or add something to my dropbox it notifies an update on webhook.site so it seems to be working on that end.
- In my main program its a script setup in pycharm using flask which i run.
The main problem is that when I update my dropbox webhook.site registers the callback however the program on my pc fails to do.
Im using windows 10 and pycharm as my ide, a basic dropbox account.
Ive included my code below, any insight would be greatly appreciated!
from hashlib import sha256
import hmac
import threading
import json
import requests
from flask import request, Response, app, abort, Flask
import os
app = Flask(__name__)
def auth():
app_key = # from app
app_secret = # from app
# build the authorization URL:
authorization_url = "https://www.dropbox.com/oauth2/authorize?client_id=%s&response_type=code" % app_key
# send the user to the authorization URL:
print('Go to the following URL and allow access:')
print(authorization_url)
# get the authorization code from the user:
authorization_code = input('Enter the code:\n')
# exchange the authorization code for an access token:
token_url = "https://api.dropboxapi.com/oauth2/token"
params = {
"code": authorization_code,
"grant_type": "authorization_code",
"client_id": app_key,
"client_secret": app_secret
}
r = requests.post(token_url, data=params)
print(r.text)
@app.route('/link to webhook.site/mywebhook', methods=['GET'])
def verify():
print("verify")
resp = Response(request.args.get('webhook.site challenge goes here'))
resp.headers['Content-Type'] = 'text/plain'
resp.headers['X-Content-Type-Options'] = 'nosniff'
print(resp)
return resp
@app.route('/link to webhook.site/mywebhook', methods=['POST'])
def webhook():
print("webhook")
app_key = # from app
app_secret = # from app
# Make sure this is a valid request from Dropbox
signature = request.headers.get('X-Dropbox-Signature')
if not hmac.compare_digest(signature, hmac.new(app_secret, request.data, sha256).hexdigest()):
abort(403)
for account in json.loads(request.data)['list_folder']['accounts']:
threading.Thread(target=process_user, args=(account,)).start()
return ''
def process_user(account):
print("Hello")
if __name__ == "__main__":
auth()
app.run()OK
I authenticate the user, it takes me through the process of clicking on the windows to get a key which i then enter back into the console. If its important i can never find the localhost, always says its out of reach.
Thanks for taking the time to read my long post!