django Github中的身份验证失败|Python社会认证

iqjalb3h  于 2023-02-06  发布在  Go
关注(0)|答案(4)|浏览(168)

在我的django项目中,我使用了python-social-auth。我尝试用Github添加社交登录,但不幸的是,出现了下一个错误
验证失败: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/

    • 设置. 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'
)
xiozqbni

xiozqbni1#

尝试在http://127.0.0.1:8000/而不是http://localhost:8000/上注册回调URL

c3frrgcw

c3frrgcw2#

问题是Github的回调URL。在我的情况下,我有语言前缀内的URL,没有想到Github会敏感,因为其他供应商没有提出错误的链接没有前缀。我发现这个post描述如何显示默认语言没有路径前缀。我希望它会帮助有人谁将面临这样的问题。

jgwigjjp

jgwigjjp3#

皮卡西克说得对。
在GitHub网站中,在应用程序设置下,将“localhost”改为“127.0.0.1“,它对我有效。

gkl3eglg

gkl3eglg4#

the documentation
GitHub Developers上注册一个新的应用程序,将回调URL设置为http://example.com/complete/github/,将www.example.com替换example.com为您的域。这将生成客户端密钥和客户端机密。
问题是,这并不是每个人都能做到的。
假设您正在本地开发,并且您的www.example.com中包含以下内容urls.py

path('social/', include('social_django.urls', namespace='social')),

则需要在授权回调URL中添加以下内容

http://localhost/social/complete/github/

然后,您可以在模板中执行以下操作

<a href="{% url 'social:begin' 'github' %}">GitHub</a>

相关问题