Django的休息框架,社会的oauth2

时间:2017-11-11 18:14:23

标签: django-rest-framework

测试请求期间发生异常 -

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.

我想指出几个可能重要的事实:

我使用自定义用户模型 -     https://pastebin.com/yWSvVUbe

我在我通常的身份验证(注册和登录)中使用rest-framework-jwt,我想使用drfso2进行社交身份验证(facebook和google)。

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

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

非常感谢!!!

2 个答案:

答案 0 :(得分:0)

您必须在setting.py文件中设置DRFSO2_URL_NAMESPACE参数。

URL名称空间是放置身份验证API的位置。

检查主urls.py文件:

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')),
    ...
]

在我的示例中,您必须将其设置为'auth-api'

DRFSO2_URL_NAMESPACE='auth-api'

答案 1 :(得分:0)

我不确定您的设置和url的配置是什么,因为带有您提供的作为pastebin链接的代码的文件显然已删除并且不再可用,但是我通过以下方式解决了类似的问题。

如果对处理社交身份验证的端点使用默认的urlconf设置,则根本不会出现此问题,因此您可以使用:

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

如果您具有一些自定义的urlconf设置,并且希望将用于社交身份验证的端点放置在其他位置,则不要在设置中设置变量DRFSO2_URL_NAMESPACE,只需将 name 参数添加到urls.py: / p>

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

问题应该消失了。

希望它可以帮助某人。