Patrik
Patrik

Reputation: 450

TypeError: prepare_request_body() got multiple values for keyword argument 'redirect_uri'

im trying to use Google OAuth2 with the requests-oauthlib. I already tried to use Ajax and stuff but for logging in with the Google OAuth2 data to the Django backend it seems like i have to use a libary like this one. (Because they provide a ready to work Middleware and a authentication backend, i don't want to write that stuff)

Another problem i have is that i use class based views, but the tutorials are only showing function based views.

Well, after i got my code from Google i am trying to fetch the access token. The problem is that i just can't get it to work.

This is where the magic happens (well.. not really):

class LoginView(RedirectView):

    authorization_base_url = r'https://accounts.google.com/o/oauth2/auth'
    token_url = r'https://accounts.google.com/o/oauth2/token'
    client_id = r'Totally forgot that one'
    client_secret = r'Super, super secret'
    redirect_uri = 'http://localhost:8000/de/callback'
    scope = [r'email',
             r'https://www.googleapis.com/auth/userinfo.profile']

    def get(self, request):

        google = OAuth2Session(self.client_id,
                               redirect_uri = self.redirect_uri,
                               scope=self.scope)

        authorization_url, state = google.authorization_url(
                                    self.authorization_base_url,
                                    access_type='offline',
                                    approval_prompt="force")

        request.session['oauth_state'] = state

        return redirect(authorization_url)

This works just fine. And as next, after i come back from Google:

class CallbackView(RedirectView):

    def get(self, request):
        r = request.GET.copy()

        google = OAuth2Session(LoginView.client_id,
                               state = request.session['oauth_state'])

        authorization_response = (LoginView.redirect_uri + 
                             "/callback?state=" + request.session['oauth_state'] + 
                             "&code=" + r['code'])

        token = google.fetch_token(LoginView.token_url,
                                   authorization_response = authorization_response,
                                   client_secret = LoginView.client_secret,
                                   redirect_uri = LoginView.redirect_uri,
                                   )

    ...

And ofcourse my Traceback:

Traceback:
File "/home/patrik/.virtualenvs/cms/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/patrik/.virtualenvs/cms/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/home/patrik/.virtualenvs/cms/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "/home/patrik/Schreibtisch/cms/cmsproject/apps/topics/views.py" in get
  106.                                    redirect_uri = LoginView.redirect_uri,
File "/home/patrik/.virtualenvs/cms/local/lib/python2.7/site-packages/requests_oauthlib/oauth2_session.py" in fetch_token
  167.                 password=password, **kwargs)

Exception Type: TypeError at /de/callback
Exception Value: prepare_request_body() got multiple values for keyword argument 'redirect_uri'

Where does that Error come from?

Upvotes: 2

Views: 2000

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122352

The fetch_token() method should not be passed a redirect_uri parameter. It will provide this parameter from the instance attribute google.redirect_uri instead.

In other words, give it to the OAuth2Session constructor instead:

google = OAuth2Session(
    LoginView.client_id,
    state = request.session['oauth_state'],
    redirect_uri = LoginView.redirect_uri)

Upvotes: 3

Related Questions