0

在我的django项目中,我使用python-social-auth。我特林添加社交登录与Github上只可惜旁边错误Github认证失败python-social-auth

验证失败:REDIRECT_URI必须匹配注册 回调URL这种应用。

完整的错误是这样的:

Traceback (most recent call last): 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py", line 39, in inner 
    response = get_response(request) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response 
    response = self.process_exception_by_middleware(e, request) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response 
    response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func 
    response = view_func(request, *args, **kwargs) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view 
    return view_func(*args, **kwargs) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\social_django\utils.py", line 50, in wrapper 
    return func(request, backend, *args, **kwargs) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\social_django\views.py", line 28, in complete 
    redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\social_core\actions.py", line 41, in do_complete 
    user = backend.complete(user=user, *args, **kwargs) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\social_core\backends\base.py", line 39, in complete 
    return self.auth_complete(*args, **kwargs) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\social_core\utils.py", line 253, in wrapper 
    return func(*args, **kwargs) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\social_core\backends\oauth.py", line 386, in auth_complete 
    self.process_error(self.data) 
    File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\social_core\backends\oauth.py", line 379, in process_error 
    data['error']) 
social_core.exceptions.AuthFailed: Authentication failed: The redirect_uri MUST match the registered callback URL for this application. 
[27/Feb/2017 16:06:53] "GET /ru/complete/github/?error=redirect_uri_mismatch&error_description=The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application.&error_uri=https%3A%2F%2Fdeveloper.github.com%2Fv3%2Foauth%2F%23redirect-uri-mismatch&state=fEheCJ4QPZZuz7qHPQUKxvMWl2Rw4xTV HTTP/1.1" 500 105176 

我在Github上注册的应用程序并设置设置回调URL作为 http://localhost:8000/complete/github/

settings.py

INSTALLED_APPS = [ 
    *** 
    'social_django', # python-social-auth 
] 

AUTHENTICATION_BACKENDS = (
    'social_core.backends.github.GithubOAuth2', 
    'django.contrib.auth.backends.ModelBackend', 
) 

SOCIAL_AUTH_GITHUB_KEY = 'CLIENT ID' 
SOCIAL_AUTH_GITHUB_SECRET = 'Client Secret' 

MIDDLEWARE = [ 
    *** 
    'social_django.middleware.SocialAuthExceptionMiddleware', 
] 

TEMPLATES = [ 
    { 
     *** 
     'OPTIONS': { 
      'context_processors': [ 
       *** 
       'social_django.context_processors.backends', 
       'social_django.context_processors.login_redirect', 
      ], 
     }, 
    }, 
] 

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details', 
    'social.pipeline.social_auth.social_uid', 
    'social.pipeline.social_auth.auth_allowed', 
    'social.pipeline.social_auth.social_user', 
    'social.pipeline.user.get_username', 
    'social.pipeline.mail.mail_validation', 
    'social.pipeline.social_auth.associate_by_email', 
    'social.pipeline.user.create_user', 
    'social.pipeline.social_auth.associate_user', 
    'social.pipeline.social_auth.load_extra_data', 
    'social.pipeline.user.user_details' 
) 
+0

你在github应用程序的开发者页面上注册了http:// localhost:8000/complete/github/as回调url吗? –

+0

@AjayGupta是的,当然。 –

+0

你可以粘贴你放在那里的东西吗? –

回答

0

的问题是与Github上的回调URL。在我的情况下,我有URL内的语言前缀,没想到Github会对这种原因敏感,其他提供者没有提出没有前缀的链接错误。我发放这个post,它描述了如何显示没有路径前缀的默认语言。我希望它能帮助那些会遇到这样问题的人。