Authentication failed in Github | python-social-auth
Asked Answered
E

4

2

In my django project I use python-social-auth. I am tring to add social login with Github but unfortunately have next error:

Authentication failed: The redirect_uri MUST match the registered callback URL for this application.

Full error looks like this:

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

I registered application in Github and set set the callback URL as 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'
)
Epilate answered 27/2, 2017 at 10:21 Comment(8)
have you registered localhost:8000/complete/github as callback url on your github application's developer page?Carbonic
@AjayGupta yes, sure.Epilate
could you paste what have you put there?Carbonic
Also i would suggest you to look into this #23360660 . If it is not solved, let me knowCarbonic
Maybe you didnt notice but in the middle of my post I wrote that adress localhost:8000/complete/githubEpilate
That link didnt help me... For thats why I asked for help сause didnt understand why this error raise =(Epilate
Finally, I found mistake. It was really in callback URL of GitHub. I have language prefix in my URL. The problem now I have 2 language versions (localhost:8000/ru... and localhost:8000/en/...) of site but in Github you can set only one of them. How do you think I can solve this problem?Epilate
I don't even have any language prefix, still same error. Seems i have a long night ahead...Frazzle
J
5

Try to registrate callback URL on http://127.0.0.1:8000/ instead http://localhost:8000/

Ji answered 28/8, 2020 at 16:49 Comment(1)
your explanation is so simple write more about your answare for better understand.Walliw
B
1

Pichkasik is right.

In the GitHub website, under application setting, change "localhost" with "127.0.0.1" and it works for me.

Authorization callback url setting on Github

Bennir answered 18/8, 2022 at 20:49 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewKennith
E
0

The problem was with callback URL of Github. In my case I had language prefix inside URL, didnt expect that Github would be sensitive to that cause other provideres didnt raise error for link without prefix. I fount this post which describe how to show default language without path prefixes. I hope it will help someone who would face with such problem.

Epilate answered 27/2, 2017 at 20:37 Comment(0)
A
0

In the documentation

Register a new application at GitHub Developers, set the callback URL to http://example.com/complete/github/ replacing example.com with your domain. This will generate a Client Key and a Client Secret.

Thing is, it's not http://example.com/complete/github/ for everyone.


Let's say you're developing locally and you have the following in your urls.py

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

then you'll need to add in your Authorization callback URL the following

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

enter image description here

You can then do something like this in your template

<a href="{% url 'social:begin' 'github' %}">GitHub</a>
Acrylyl answered 31/1, 2023 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.