TypeError:prepare_request_body()获得了关键字参数' redirect_uri'的多个值。

时间:2015-03-16 14:27:16

标签: python django google-oauth typeerror django-class-based-views

我尝试将Google OAuth2与requests-oauthlib一起使用。 我已经尝试过使用Ajax和东西,但是为了使用Google OAuth2数据登录到Django后端,似乎我必须使用像这样的库。 (因为它们提供了准备工作的中间件和身份验证后端,我不想写那些东西)

我遇到的另一个问题是我使用基于类的视图,但教程只显示基于函数的视图。

好吧,在我从Google获取代码后,我正在尝试获取访问令牌。问题是我无法让它发挥作用。

这就是魔术发生的地方(好吧......不是真的):

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)

这很好用。 接下来,在我从谷歌回来之后:

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,
                                   )

    ...

当然是我的追溯:

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'

错误来自哪里?

1 个答案:

答案 0 :(得分:3)

fetch_token()方法不应传递redirect_uri参数。它将从实例属性google.redirect_uri中提供此参数。

换句话说,请将其提交给OAuth2Session构造函数:

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