我正在将 python-social-auth 与 Django 一起使用,以使用 Oauth2 (google-oauth2) 向 Google 进行身份验证。在我的模板中我使用了一些东西like https://github.com/omab/python-social-auth/blob/master/examples/django_example/example/templates/home.html:
<a href="{% url "begin:social" "google-oauth2" %}">Login</a>
它正确地创建了一个A
指向正确登录 URL 的元素。问题是,django 正在运行runserver
Apache 后面的选项ProxyPass
and ProxyPassReverse
(也尝试过 nginxproxy_pass
选项,不是 fcgi)。
在这种情况下我有两个问题;首先,在创建身份验证请求时,python-social-auth 发送参数redirect_uri
as localhost:8000
所以我在settings.py上做了这个:
AUTH_EXTRA_ARGUMENTS = {'redirect_uri': 'http://myapp.com'}
它绕过了如果我发送则返回的“错误请求”Google 响应localhost:8000
作为redirect_uri。
然后,在 python-social-auth 中oauth.py
, here https://github.com/omab/python-social-auth/blob/master/social/backends/oauth.py#L319, 我理解了
self.auth_complete_params(self.validate_state())
发送同样错误吗redirect_uri
去谷歌。所以我做了一些丑陋的事情,比如:
data = self.auth_complete_params(self.validate_state())
data['redirect_uri'] = 'http://myapp.com'
response = self.request_access_token(
self.ACCESS_TOKEN_URL,
data=data, # ugh!
headers=self.auth_headers(),
method=self.ACCESS_TOKEN_METHOD
)
所以,这就是我当前的解决方案。我只是不明白如何以更优雅的方式或“正确”的方式做到这一点。
有什么帮助吗?
Best