Cut the Clutter: Test Ignore Files Feature - sign up to become a beta tester here.

Forum Discussion

pyraxic's avatar
pyraxic
Helpful | Level 6
8 years ago

Python sdk error DropboxAuth2Flow

I am using python sdk to list and download files but getting stuck at authetication. Using this code to authenticate user to allow access.

 

from dropbox import DropboxOAuth2Flow, oauth
import http_status

APP_KEY = 'mykey'
APP_SECRET = 'mysecretkey'

def get_dropbox_auth_flow(web_app_session):
redirect_uri = "https://my-web-server.org/dropbox-auth-finish"
return DropboxOAuth2Flow(
APP_KEY, APP_SECRET, redirect_uri, web_app_session,
"dropbox-auth-csrf-token")

# URL handler for /dropbox-auth-start
def dropbox_auth_start(web_app_session, request):
authorize_url = get_dropbox_auth_flow(web_app_session).start()
redirect_to(authorize_url)

# URL handler for /dropbox-auth-finish
def dropbox_auth_finish(web_app_session, request):
try:
oauth_result = \
get_dropbox_auth_flow(web_app_session).finish(
request.query_params)
except oauth.BadRequestException as e:
http_status(400)
except oauth.BadStateException as e:
# Start the auth flow again.
redirect_to("/dropbox-auth-start")
except oauth.CsrfException as e:
http_status(403)
except oauth.NotApprovedException as e:
flash('Not approved? Why not?')
return redirect_to("/home")
except oauth.ProviderException as e:
logger.log("Auth error: %s" % (e,))
http_status(403)

Following: https://dropbox-sdk-python.readthedocs.io/en/latest/moduledoc.html#module-dropbox.oauth

 

Getting "Unresolved reference 'redirect_to'" same with logger and flash. I also get 'http_status' is not callable. Do I need Flask to run this code? I am trying to build a simple python script that will let user authenticate using browser.

 

2 Replies

  • pyraxic's avatar
    pyraxic
    Helpful | Level 6
    8 years ago

    Okay, so I tried importing all the modules required to run this script as below and there are no errors.

     

    from dropbox import DropboxOAuth2Flow, oauth
    import http_status
    from flask import flash

    APP_KEY = 'mykey'
    APP_SECRET = 'mysecretkey'

    def get_dropbox_auth_flow(web_app_session):
    redirect_uri = "https://my-web-server.org/dropbox-auth-finish"
    return DropboxOAuth2Flow(
    APP_KEY, APP_SECRET, redirect_uri, web_app_session,
    "dropbox-auth-csrf-token")

    # URL handler for /dropbox-auth-start
    def dropbox_auth_start(web_app_session, request):
    authorize_url = get_dropbox_auth_flow(web_app_session).start()
    redirect_to(authorize_url)

    # URL handler for /dropbox-auth-finish
    def dropbox_auth_finish(web_app_session, request):
    try:
    oauth_result = \
    get_dropbox_auth_flow(web_app_session).finish(
    request.query_params)
    except oauth.BadRequestException as e:
    http_status(400)
    except oauth.BadStateException as e:
    # Start the auth flow again.
    redirect_to("/dropbox-auth-start")
    except oauth.CsrfException as e:
    http_status(403)
    except oauth.NotApprovedException as e:
    flash('Not approved? Why not?')
    return redirect_to("/home")
    except oauth.ProviderException as e:
    logger.log("Auth error: %s" % (e,))
    http_status(403)

    But it doesn't seem to open up the browser for authentication from the user. Am I missing something?

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    It looks like you're referring to the example code in the DropboxOAuth2Flow documentation. That example code isn't a complete sample, so it won't work on its own. For instance, it doesn't include definitions for `redirect_to` or `http_status`. It's just meant as an outline of how you would implement this in a web app using a web framework.

     

    First, to clarify, are you building a web app using a web framework? You don't need to use Flask in particular, but the expectation for that sample is that you would be using some framework that provides the functionality for `redirect_to` or `http_status` (or that you would build your own). In that case, the line `redirect_to(authorize_url)` would send the user to the Dropbox OAuth app authorization page.

     

    You said that you're "trying to build a simple python script that will let user authenticate using browser", so it sounds like you may not be building a web app, but rather a local app. In that case, you would still need to have the use the browser to have the user authorize the app, but you're not in a context where you can just redirect them there. In that case, DropboxOAuth2FlowNoRedirect may make more sense for your use case. There's a small sample in the documentation for that too that may be helpful.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,038 PostsLatest Activity: 2 hours ago
415 Following

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!