cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Share your feedback on the Document Scanning Experience in the Dropbox App right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Webhook Tutorial Error

Webhook Tutorial Error

af11
Explorer | Level 4
Go to solution

I downloaded the code (https://github.com/dropbox/mdwebhook) and am trying to make this application work on my IDE.

The site is up but when I press on the button to connect to Dropbox, I get this error "TypeError: 'SecureCookieSession' object cannot be interpreted as an integer".

Can anyone help me solve this?

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

Thanks, that's helpful. It looks like you're running this sample project with the latest version of the 'dropbox' package (currently v11.25.0), however this sample project was only written for an older version. Attempting to use it with the latest version will fail like this due to subsequent changes in the 'dropbox' package.

 

So, to run this sample project, you should install the project requirements as listed in the project here (which specifies 'dropbox==9.4.0'). The instructions for the sample project show how to install the requirements. (You may want to use something like virtualenv though.)

View solution in original post

4 Replies 4

Greg-DB
Dropbox Staff
Go to solution

The 'mdwebhook' project doesn't contain any reference of 'SecureCookieSession', so can you share the full stack trace and relevant code for context? Thanks!

af11
Explorer | Level 4
Go to solution

 

from hashlib import sha256
import hmac
import json
import os
import threading
import urllib.parse
from dotenv import load_dotenv

from dropbox import Dropbox, DropboxOAuth2Flow
from dropbox.files import DeletedMetadata, FolderMetadata, WriteMode
from flask import abort, Flask, redirect, render_template, Response, request, session, url_for
from markdown import markdown
import redis

load_dotenv()

redis_url = os.environ.get('REDISTOGO_URL')
redis_client = redis.from_url(redis_url, decode_responses=True)

# App key and secret from the App console (dropbox.com/developers/apps)
APP_KEY = os.environ.get('APP_KEY')
APP_SECRET = os.environ.get('APP_SECRET')

app = Flask(__name__)
app.debug = True

# A random secret used by Flask to encrypt session data cookies
app.secret_key = os.environ.get('FLASK_SECRET_KEY')

def get_url(route):
    '''Generate a proper URL, forcing HTTPS if not running locally'''
    host = urllib.parse.urlparse(request.url).hostname
    url = url_for(
        route,
        _external=True,
        _scheme='http' if host in ('127.0.0.1', 'localhost') else 'https'
    )

    return url

def get_flow():
    return DropboxOAuth2Flow(
        APP_KEY,
        APP_SECRET,
        get_url('oauth_callback'),
        session,
        'dropbox-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)

@app.route('/oauth_callback')
def oauth_callback():
    '''Callback function for when the user returns from OAuth.'''

    auth_result = get_flow().finish(request.args)
    account = auth_result.account_id
    access_token = auth_result.access_token

    # Extract and store the access token for this user
    redis_client.hset('tokens', account, access_token)

    process_user(account)

    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(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, DeletedMetadata) or
                isinstance(entry, 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.decode("utf-8"))
            dbx.files_upload(bytes(html, encoding='utf-8'), entry.path_lower[:-3] + '.html', mode=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')

@app.route('/login')
def login():
    return redirect(get_flow().start())

@app.route('/done')
def done():
    return render_template('done.html')

@app.route('/webhook', methods=['GET'])
def challenge():
    '''Respond to the webhook challenge (GET request) by echoing back the challenge parameter.'''

    resp = Response(request.args.get('challenge'))
    resp.headers['Content-Type'] = 'text/plain'
    resp.headers['X-Content-Type-Options'] = 'nosniff'

    return resp

@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
    signature = request.headers.get('X-Dropbox-Signature')
    key = bytes(APP_SECRET, encoding="ascii")
    computed_signature = hmac.new(key, request.data, sha256).hexdigest()
    if not hmac.compare_digest(signature, computed_signature):
        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)

 

and here is the traceback to the error:

TypeError

TypeError: 'SecureCookieSession' object cannot be interpreted as an integer

Traceback (most recent call last)

 
  • File "C:\Users\Sari\PycharmProjects\ccc-changes\venv\Lib\site-packages\flask\app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  • File "C:\Users\Sari\PycharmProjects\ccc-changes\venv\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  • File "C:\Users\Sari\PycharmProjects\ccc-changes\venv\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  • File "C:\Users\Sari\PycharmProjects\ccc-changes\venv\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  • File "C:\Users\Sari\PycharmProjects\ccc-changes\venv\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  • File "C:\Users\Sari\PycharmProjects\ccc-changes\venv\Lib\site-packages\flask\app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  • File "C:\Users\Sari\PycharmProjects\ccc-changes\app.py", line 112, in login
    return redirect(get_flow().start())
  • File "C:\Users\Sari\PycharmProjects\ccc-changes\venv\Lib\site-packages\dropbox\oauth.py", line 440, in start
    self.session[self.csrf_token_session_key] = csrf_token
TypeError: 'SecureCookieSession' object cannot be interpreted as an integer
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.

To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

  • dump() shows all variables in the frame
  • dump(obj) dumps all that's known about the object

 

 

Thank you so much!

Greg-DB
Dropbox Staff
Go to solution

Thanks, that's helpful. It looks like you're running this sample project with the latest version of the 'dropbox' package (currently v11.25.0), however this sample project was only written for an older version. Attempting to use it with the latest version will fail like this due to subsequent changes in the 'dropbox' package.

 

So, to run this sample project, you should install the project requirements as listed in the project here (which specifies 'dropbox==9.4.0'). The instructions for the sample project show how to install the requirements. (You may want to use something like virtualenv though.)

af11
Explorer | Level 4
Go to solution

Thank you so much!

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    af11 Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?