python-social-auth / social-core

Python Social Auth - Core
BSD 3-Clause "New" or "Revised" License
844 stars 544 forks source link

Reddit complete raises forbidden error #558

Closed dwaxe closed 3 years ago

dwaxe commented 3 years ago

Expected behaviour

The Reddit completion backend should complete successfully, create a new user, all that jazz. I hope this doesn't come across as too much of a "help I'm confused" issue, but I've gotten other backends working without issue (Spotify, Twitter).

Actual behaviour

After going to /login/reddit (which redirects to https://ssl.reddit.com/api/v1/authorize) and authorizing the app, the redirect to /complete/reddit errors with a 403 response. I'm guessing Reddit expects the request headers to be different.

What are the steps to reproduce this issue?

  1. Create a Reddit web app at https://www.reddit.com/prefs/apps
  2. Set the callback url to http://127.0.0.1:8000/complete/reddit/ or your desired url
  3. Django specific steps follow:
  4. Add social_core.backends.reddit.RedditOAuth2 to your AUTHENTICATION_BACKENDS in settings.py
  5. Set SOCIAL_AUTH_REDDIT_KEY and SOCIAL_AUTH_REDDIT_SECRET vars in settings.py
  6. Add path('', include('social_django.urls', namespace='social')) to urls.py
  7. Run migrations (./manage.py migrate), any other required setup
  8. ./manage.py runserver
  9. Visit http://127.0.0.1:8000/login/reddit/, which will redirect you to Reddit's OAuth screen
  10. Click Allow
  11. The following traceback appears

Any logs, error output, etc?

Traceback (most recent call last):
  File "/.venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/.venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/.venv/lib/python3.8/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/.venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/.venv/lib/python3.8/site-packages/social_django/utils.py", line 49, in wrapper
    return func(request, backend, *args, **kwargs)
  File "/.venv/lib/python3.8/site-packages/social_django/views.py", line 31, in complete
    return do_complete(request.backend, _do_login, user=request.user,
  File "/.venv/lib/python3.8/site-packages/social_core/actions.py", line 45, in do_complete
    user = backend.complete(user=user, *args, **kwargs)
  File "/.venv/lib/python3.8/site-packages/social_core/backends/base.py", line 40, in complete
    return self.auth_complete(*args, **kwargs)
  File "/.venv/lib/python3.8/site-packages/social_core/utils.py", line 248, in wrapper
    return func(*args, **kwargs)
  File "/.venv/lib/python3.8/site-packages/social_core/backends/oauth.py", line 391, in auth_complete
    return self.do_auth(response['access_token'], response=response,
  File "/.venv/lib/python3.8/site-packages/social_core/utils.py", line 248, in wrapper
    return func(*args, **kwargs)
  File "/.venv/lib/python3.8/site-packages/social_core/backends/oauth.py", line 397, in do_auth
    data = self.user_data(access_token, *args, **kwargs)
  File "/.venv/lib/python3.8/site-packages/social_core/backends/reddit.py", line 38, in user_data
    return self.get_json(
  File "/.venv/lib/python3.8/site-packages/social_core/backends/base.py", line 241, in get_json
    return self.request(url, *args, **kwargs).json()
  File "/.venv/lib/python3.8/site-packages/social_core/backends/base.py", line 237, in request
    response.raise_for_status()
  File "/.venv/lib/python3.8/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)

Exception Type: HTTPError at /complete/reddit/
Exception Value: 403 Client Error: Forbidden for url: https://oauth.reddit.com/api/v1/me.json
dwaxe commented 3 years ago

Nevermind, I realized I just didn't request the "identity" scope that is required to access Reddit's api/v1/me.json