Hannan
Hannan

Reputation: 1191

Dropbox API with Django - Authentication

I'm trying to learn Dropbox API and want to use OAuth 2 for authorization. I'm getting following error:

dropbox_auth_start() missing 1 required positional argument: 'request'

Here is my code:

Views.py

from dropbox import DropboxOAuth2Flow
from django.shortcuts import redirect

def get_dropbox_auth_flow(web_app_session):
    redirect_uri = "https://www.my-dummy-url.com"
    APP_KEY = 'my-app-key'
    APP_SECRET = 'my-app-secret'
    return DropboxOAuth2Flow(
        APP_KEY, APP_SECRET, redirect_uri, web_app_session, "dropbox-auth-csrf-token")

def dropbox_auth_start(web_app_session, request):
    if request.method == 'GET':
        authorize_url = get_dropbox_auth_flow(web_app_session).start()
        return redirect(authorize_url)

urls.py

urlpatterns = [
    path('dropbox/', views.dropbox_auth_start, name='dropbox')
]

Upvotes: 1

Views: 652

Answers (2)

iamlordaubrey
iamlordaubrey

Reputation: 21

An aside... as @at14 said, request object needs to be the first argument. The first argument is the request object (in this case, web_app_session is the request object). The second argument called 'request' (in the dropbox_auth_start function) is not needed.

More to the point, session object needs to be called on the request object like so: web_app_session.session

def dropbox_auth_start(web_app_session):
    authorize_url = get_dropbox_auth_flow(web_app_session.session).start()
    return redirect(authorize_url)

Essentially, web_app_session == request, so adding the if block, you'd get:

def dropbox_auth_start(web_app_session):
    if web_app_session.method == 'GET':
        authorize_url = get_dropbox_auth_flow(web_app_session.session).start()
        return redirect(authorize_url)

Of course, web_app_session can be switched with request.

Upvotes: 2

at14
at14

Reputation: 1204

request object needs to be the first argument to your function

def dropbox_auth_start(request, web_app_session):
    if request.method == 'GET':
        authorize_url = get_dropbox_auth_flow(web_app_session).start()
        return redirect(authorize_url)

Upvotes: 1

Related Questions