我从the google cloud api console创建了客户端ID和客户端密钥,并在社交应用表中添加了django-allauth
我还添加了WEB ORIGIN
:
mysite.com
(prod)http://localhost:8000
(dev)和REDIRECT URI
http:mysite.com/accounts/google/login/callback/
(prod)localhost:8000/accounts/google/login/callback/
(dev)在google api控制台中。
使用Google登录可以很好地开发,并在成功登录时重定向到localhost回调网址。但是我在生产中遇到redirect_uri_mismatch
错误。
以下是Google错误页面中的错误详情:
请求详细信息
cookie_policy_enforce=false
scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
response_type=code
access_type=online
redirect_uri=http://127.0.0.1:8000/accounts/google/login/callback/
state=SOME_RANDOM_NUMBER
display=page
client_id=MY_CLIENT_ID
redirect_uri
仍设为127.0.0.1
而非http:mysite.com/accounts/google/login/callback/
那么如何设置正确的redirect_uri
?
这是与settings.py
django-allauth
INSTALLED_APPS = (
#the usual stuff
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
)
import django.conf.global_settings as DEFAULT_SETTINGS
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
)
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
ACCOUNT_EMAIL_REQUIRED = True
LOGIN_REDIRECT_URL = "/"
这是urls.py
urlpatterns = patterns('',
url(r'^accounts/', include('allauth.urls')),
)
我没有进行任何其他django-allauth
设置。
我看到了docs,无法找到进行更改的位置。
答案 0 :(得分:8)
我发现问题的发生是因为位于python app服务器前面的nginx代理将HTTP Host
标头设置为localhost
。
因此当allauth
尝试request.build_absolute_uri
时,HTTP_HOST
为localhost
。
所以我在解决问题的nginx configuration file中设置了设置proxy_set_header
。
proxy_set_header Host $http_host;
另请参阅Facebook oauth authentication is redirecting to localhost instead of to my domain了解不同头像中的同一问题。