Cut the Clutter: Test Ignore Files Feature - sign up to become a beta tester here.
Forum Discussion
farzad2004
8 years agoExplorer | Level 3
Issue with demo webhoook app running at mdwebhook.herokuapp.com
Dear all,
I need to use webhook in one of our projects. I have carefully read webhook documentation available at https://www.dropbox.com/developers/reference/webhooks. Within th tuturial there is demo webhook deployed at mdwebhook.herokuapp.com but is not working. When I open the app and connect to my dropbox account it migrates to an error page as shown in the below screenshot. I also deployed the same app at heroku myself and receive the same error. Could someone help me to rectify this issue? Thank you.
Kindest Regards,
Farzad
6 Replies
- Greg-DB8 years ago
Dropbox Community Moderator
Thanks for the report Farzad! It looks like we haven't updated the source code on GitHub or the deployed version for API v2, so it wasn't currently working. I temporarily re-enabled it, so it should be working again. I'll ask the team to update the source and deployed app.
Please use the code from the documentation itself though, which is updated for API v2:
https://www.dropbox.com/developers/reference/webhooks - farzad20048 years agoExplorer | Level 3
Thanks dear Greg, I manage to change the github codebase based on the code from the webhook documentation. BTW, I didn't get what you mean by termporarily re-enable it as both the demo webhook app and github are still not working. Thanks.
Kindest Regards,
Farzad
- Greg-DB8 years ago
Dropbox Community Moderator
Can you elaborate on what exactly you mean when you say they are "still not working"? Are you getting the same "Internal Server Error" as before? I re-enabled some functionality that was disabled and causing it to fail before. It's now working properly for me.
In any case, is the updated code working for you? If so, please use that as your reference. - farzad20048 years agoExplorer | Level 3
Hi Greg,
Many thanks for your response. I used my business dropbox account and it did not working (i.e., throwing the same internal server error). Today, I checked with my personal account and it worked.
Now, I am trying to change the mdwebhook github code based on the documentation at https://www.dropbox.com/developers/reference/webhooks but haven't been succeded as documentation does not include all the code functions (I receive several OAuth error while deploying the code). My changed code is available at https://github.com/farzad2004/qldwebhook/blob/master/app.py. I am wondering if you could direct me to the right direction about what I am missing while changing the code. Again, thank you.
Kindest Regards,
Farzad
- Greg-DB8 years ago
Dropbox Community Moderator
I'm not sure I understand your latest question. Can you share the steps to reproduce the issue, and the full error/output you're getting? Thanks in advance! - farzad20048 years agoExplorer | Level 3
Hi Greg,
Many thanks for your response. After hours of working on updating the webhook source code based on API V2.0, I could finally come up with a working code. I put the content of app.py file (as the main file that needed to be updated in below) for the reference of other people who might have the same problem. Again, thank you.
from hashlib import sha256
import hmac
import json
import os
import threading
import urlparse
from dropbox import DropboxOAuth2Flow, oauth
from flask import abort, Flask, redirect, render_template, request, session, url_for
from markdown import markdown
import redis
import dropbox
redis_url = os.environ['REDISTOGO_URL']
redis_client = redis.from_url(redis_url)
# App key and secret from the App console (dropbox.com/developers/apps)
APP_KEY = os.environ['APP_KEY']
APP_SECRET = os.environ['APP_SECRET']
app = Flask(__name__)
app.debug = True
# A random secret used by Flask to encrypt session data cookies
app.secret_key = os.environ['FLASK_SECRET_KEY']
def get_url(route):
'''Generate a proper URL, forcing HTTPS if not running locally'''
host = urlparse.urlparse(request.url).hostname
url = url_for(
route,
_external=True,
_scheme='http' if host in ('127.0.0.1', 'localhost') else 'https'
)
print url
return url
def get_flow():
return DropboxOAuth2Flow(
APP_KEY, APP_SECRET, get_url('oauth_callback'), session,
"dropbox-auth-csrf-token")
@app.route('/welcome')
def welcome():
return render_template('welcome.html', redirect_url=get_url('oauth_callback'),
webhook_url=get_url('webhook'), home_url=get_url('index'), app_key=APP_KEY)
# URL handler for /dropbox-auth-finish
@app.route('/oauth_callback')
def oauth_callback():
oauth_result = get_flow().finish(request.args)
print oauth_result
redis_client.hset('tokens', oauth_result.account_id, oauth_result.access_token)
process_user(oauth_result.account_id)
return redirect(url_for('done'))
def process_user(account):
'''Call /files/list_folder for the given user ID and process any changes.'''
# OAuth token for the user
token = redis_client.hget('tokens', account)
# cursor for the user (None the first time)
cursor = redis_client.hget('cursors', account)
dbx = dropbox.Dropbox(token)
has_more = True
while has_more:
if cursor is None:
result = dbx.files_list_folder(path='')
else:
result = dbx.files_list_folder_continue(cursor)
for entry in result.entries:
# Ignore deleted files, folders, and non-markdown files
if (isinstance(entry, dropbox.files.DeletedMetadata) or
isinstance(entry, dropbox.files.FolderMetadata) or
not entry.path_lower.endswith('.md')):
continue
# Convert to Markdown and store as <basename>.html
_, resp = dbx.files_download(entry.path_lower)
html = markdown(resp.content)
f = open('html1.html', 'w')
f.write(html.encode('utf8'))
f.close()
with open('html1.html', "rb") as f:
dbx.files_upload(f.read(), entry.path_lower[:-3] + '.html', mode=dropbox.files.WriteMode('overwrite'))
# Update cursor
cursor = result.cursor
redis_client.hset('cursors', account, cursor)
# Repeat only if there's more to do
has_more = result.has_more
@app.route('/')
def index():
return render_template('index.html')
# URL handler for /dropbox-auth-start
@app.route('/login')
def login():
authorize_url = get_flow().start()
return redirect(authorize_url)
@app.route('/done')
def done():
return render_template('done.html')
@app.route('/webhook', methods=['GET'])
def verify():
'''Respond to the webhook verification (GET request) by echoing back the challenge parameter.'''
return request.args.get('challenge')
def validate_request():
'''Validate that the request is properly signed by Dropbox.
(If not, this is a spoofed webhook.)'''
signature = request.headers.get('X-Dropbox-Signature')
return signature == hmac.new(APP_SECRET, request.data, sha256).hexdigest()
@app.route('/webhook', methods=['POST'])
def webhook():
'''Receive a list of changed user IDs from Dropbox and process each.'''
# Make sure this is a valid request from Dropbox
# Make sure this is a valid request from Dropbox
if not validate_request(): abort(403)
for account in json.loads(request.data)['list_folder']['accounts']:
# We need to respond quickly to the webhook request, so we do the
# actual work in a separate thread. For more robustness, it's a
# good idea to add the work to a reliable queue and process the queue
# in a worker process.
threading.Thread(target=process_user, args=(account,)).start()
return ''
if __name__ == '__main__':
app.run(debug=True)
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,039 PostsLatest Activity: 9 hours ago
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
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!