测试请求期间发生异常 -
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)。
设置.py - https://pastebin.com/nqnbNusT
v1/urls.py - https://pastebin.com/vu9Lghrq
非常感谢!!!
您必须在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'
我不确定您的设置和网址的配置是什么,因为带有您提供的作为 Pastebin 链接的代码的文件显然已被删除并且不再可用,但我通过以下方式处理并解决了类似的问题。
如果您对处理社交身份验证的端点使用默认的 urlconf 设置,则根本不会出现此问题,因此您可以使用:
urlpatterns = patterns(
...
(r'^auth/', include('rest_framework_social_oauth2.urls')),
)
如果您有一些自定义 urlconf 设置,并且希望将社交身份验证的端点放置在其他位置,请不要在设置中设置变量 DRFSO2_URL_NAMESPACE,只需将 name 参数添加到 urls.py 中:
path('oauth/login/', SocialLoginView.as_view(), **name='authorize'**).
问题应该消失。
希望它可以帮助别人。
兄弟,只需添加此行即可完成!
DRFSO2_URL_NAMESPACE = 'drfso2'
如果您还没有给出该模式的 url,那么就这样做,否则一切都完成了