django-allauth登录谷歌即使在生产中也会重定向到开发网址

时间:2013-11-14 23:26:05

标签: django google-oauth django-allauth

我从the google cloud api console创建了客户端ID和客户端密钥,并在社交应用表中添加了django-allauth

的记录

我还添加了WEB ORIGIN

  1. mysite.com(prod)
  2. http://localhost:8000(dev)
  3. REDIRECT URI

    1. http:mysite.com/accounts/google/login/callback/(prod)
    2. localhost:8000/accounts/google/login/callback/(dev)
    3. 在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,无法找到进行更改的位置。

1 个答案:

答案 0 :(得分:8)

我发现问题的发生是因为位于python app服务器前面的nginx代理将HTTP Host标头设置为localhost

因此当allauth尝试request.build_absolute_uri时,HTTP_HOSTlocalhost

所以我在解决问题的nginx configuration file中设置了设置proxy_set_header

proxy_set_header Host $http_host;

另请参阅Facebook oauth authentication is redirecting to localhost instead of to my domain了解不同头像中的同一问题。