【发布时间】:2016-03-28 00:19:19
【问题描述】:
我在 Django 和 Django Rest Framework 中编写了一个应用程序,并使用 Django OAuth Toolkit 添加了 OAuth 身份验证。我一直在关注Django OAuth Toolkit getting started guide。
现在,当我尝试使用以下命令请求 access_token 时:
curl -X POST -d "grant_type=password&username=myuser&password=mypass" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
我得到如下回溯:
Dispatching grant_type password request to <oauthlib.oauth2.rfc6749.grant_types.resource_owner_password_credentials.ResourceOwnerPasswordCredentialsGrant object at 0x7f80420bbb90>.
Authenticating client, <oauthlib.common.Request object at 0x7f804332bcd0>.
Internal Server Error: /o/token/
Traceback (most recent call last):
File "/home/user/Projects/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/Projects/venv/lib/python2.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/user/Projects/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 34, in _wrapper
return bound_func(*args, **kwargs)
File "/home/user/Projects/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/user/Projects/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 30, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/user/Projects/venv/lib/python2.7/site-packages/braces/views/_forms.py", line 22, in dispatch
return super(CsrfExemptMixin, self).dispatch(*args, **kwargs)
File "/home/user/Projects/venv/lib/python2.7/site-packages/django/views/generic/base.py", line 89, in dispatch
return handler(request, *args, **kwargs)
File "/home/user/Projects/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 34, in _wrapper
return bound_func(*args, **kwargs)
File "/home/user/Projects/venv/lib/python2.7/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/home/user/Projects/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 30, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/user/Projects/venv/lib/python2.7/site-packages/oauth2_provider/views/base.py", line 170, in post
url, headers, body, status = self.create_token_response(request)
File "/home/user/Projects/venv/lib/python2.7/site-packages/oauth2_provider/views/mixins.py", line 124, in create_token_response
return core.create_token_response(request)
File "/home/user/Projects/venv/lib/python2.7/site-packages/oauth2_provider/oauth2_backends.py", line 138, in create_token_response
headers, extra_credentials)
File "/home/user/Projects/venv/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/endpoints/base.py", line 61, in wrapper
return f(endpoint, uri, *args, **kwargs)
File "/home/user/Projects/venv/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/endpoints/token.py", line 93, in create_token_response
request, self.default_token_type)
File "/home/user/Projects/venv/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/grant_types/resource_owner_password_credentials.py", line 92, in create_token_response
if not self.request_validator.authenticate_client(request):
File "/home/user/Projects/venv/lib/python2.7/site-packages/oauth2_provider/oauth2_validators.py", line 179, in authenticate_client
authenticated = self._authenticate_basic_auth(request)
File "/home/user/Projects/venv/lib/python2.7/site-packages/oauth2_provider/oauth2_validators.py", line 67, in _authenticate_basic_auth
auth_string = auth_string.encode(encoding)
TypeError: encode() argument 1 must be string, not None
我正在使用 Django 版本 1.8.5、Django REST framework 版本 3.3.2 和 Django OAuth Toolkit 版本 0.10.0。
我找到了导致这种情况的一段代码:
def _authenticate_basic_auth(self, request):
"""
Authenticates with HTTP Basic Auth.
Note: as stated in rfc:`2.3.1`, client_id and client_secret must be encoded with
"application/x-www-form-urlencoded" encoding algorithm.
"""
auth_string = self._extract_basic_auth(request)
if not auth_string:
return False
try:
encoding = request.encoding
except AttributeError:
encoding = 'utf-8'
# Encode auth_string to bytes. This is needed for python3.2 compatibility
# because b64decode function only supports bytes type in input.
if isinstance(auth_string, six.string_types):
auth_string = auth_string.encode(encoding)
[...]
但不知道为什么encoding 的值是None。如果我的项目中的任何文件有帮助,我会提供。
【问题讨论】:
-
尝试在 curl 中添加 -F?有注释""" 使用 HTTP Basic Auth 进行身份验证。注意:如 rfc:
2.3.1中所述,client_id 和 client_secret 必须使用 "application/x-www-form-urlencoded" 编码算法进行编码。""" -
试过了,还尝试手动添加 Contenty-Type 标头,但结果相同。
-
docs.djangoproject.com/en/1.9/ref/request-response/… ,检查标题中的 DEFAULT_CHARSET 和字符集。它们一定是不同的。我认为这是 django-oauth 的错误,因为根据 Django 文档编码可能为 None。
标签: python django oauth-2.0 django-rest-framework django-1.8