Denzil
Denzil

Reputation: 31

Django-rest-framework-social-oauth2

Exception happened during testing request -

curl -X POST -d "client_id=<client_id>&client_secret=
<client_secret>&grant_type=password&username=<user_name>&password=
<password>" http://localhost:8000/api/v1/auth/token

Traceback:  

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/braces/views/_forms.py" in dispatch
  24.         return super(CsrfExemptMixin, self).dispatch(*args, **kwargs)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
  489.             response = self.handle_exception(exc)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
  449.             self.raise_uncaught_exception(exc)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
  486.             response = handler(request, *args, **kwargs)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/rest_framework_social_oauth2/views.py" in post
  42.         url, headers, body, status = self.create_token_response(request._request)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/oauth2_provider/views/mixins.py" in create_token_response
  125.         return core.create_token_response(request)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/oauth2_provider/oauth2_backends.py" in create_token_response
  139.                                                                   headers, extra_credentials)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/endpoints/base.py" in wrapper
  64.             return f(endpoint, uri, *args, **kwargs)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/endpoints/token.py" in create_token_response
  117.             request, self.default_token_type)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/grant_types/resource_owner_password_credentials.py" in create_token_response
  100.             self.validate_token_request(request)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/grant_types/resource_owner_password_credentials.py" in validate_token_request
  178.                                                     request.password, request.client, request):

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/oauth2_provider/oauth2_validators.py" in validate_user
  520.         u = authenticate(username=username, password=password)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/contrib/auth/__init__.py" in authenticate
  68.     for backend, backend_path in _get_backends(return_tuples=True):

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/contrib/auth/__init__.py" in _get_backends
  29.         backend = load_backend(backend_path)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/contrib/auth/__init__.py" in load_backend
  23.     return import_string(path)()

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/utils/module_loading.py" in import_string
  20.     module = import_module(module_path)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/importlib/__init__.py" in import_module
  126.     return _bootstrap._gcd_import(name[level:], package, level)

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/rest_framework_social_oauth2/backends.py" in <module>
  7. class DjangoOAuth2(BaseOAuth2):

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/rest_framework_social_oauth2/backends.py" in DjangoOAuth2
  11.                                 if DRFSO2_URL_NAMESPACE else 'authorize')

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/urls/base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "/Users/d.matveyev/.virtualenvs/dp/lib/python3.6/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
  497.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /api/v1/auth/token
Exception Value: Reverse for 'authorize' not found. 'authorize' is not a valid view function or pattern name.

I'd like to point to several probably important facts:

I work with custom user model - https://pastebin.com/yWSvVUbe

I work with rest-framework-jwt in my usual authentication (register and login) and I wanted to use drfso2 for the possibility of social authentication (facebook and google).

settings.py - https://pastebin.com/nqnbNusT

v1/urls.py - https://pastebin.com/vu9Lghrq

Thank you very much!!!

Upvotes: 2

Views: 1709

Answers (3)

ADITYA OJHA Aditya
ADITYA OJHA Aditya

Reputation: 11

Simply add this line and done!

DRFSO2_URL_NAMESPACE = 'drfso2'

And if you have not given a URL for the pattern, then do that too; otherwise, you’re all done.

Upvotes: 0

Rocckk
Rocckk

Reputation: 426

I am not sure what are the configurations of your settings and urls as the files with the code you provided as links to pastebin are apparently removed and no longer available, but I tackled and resolved the similar issue the following way.

If you use default urlconf setting for endpoints handling social authentication, this issue should not appear at all, so you can just use:

urlpatterns = patterns(
    ...
    (r'^auth/', include('rest_framework_social_oauth2.urls')),
)

If you have some custom urlconf settings and you wish to place the endpoints for social authentication elsewhere, do not set the variable DRFSO2_URL_NAMESPACE in settings, just add the name parameter to your urls.py:

path('oauth/login/', SocialLoginView.as_view(), **name='authorize'**).

And the issue should disappear.

Hope it could help someone.

Upvotes: 1

Fabio
Fabio

Reputation: 1332

You have to set the DRFSO2_URL_NAMESPACEparameter on your setting.py file.

The URL namespace is the one where you placed your auth API.

Check your main urls.py file:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/auth/', include('myproject.myauthapp.api.urls', namespace='auth-api')),
    ...
]

With my example, you have to set it to 'auth-api':

DRFSO2_URL_NAMESPACE='auth-api'

Upvotes: 2

Related Questions