When trying to request for OAuth access_token I get: TypeError: encode() argument 1 must be string, not None
When trying to request for OAuth access_token I get: TypeError: encode() argument 1 must be string, not None
我用 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 框架版本 3.3.2 和 Django OAuth 工具包版本 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
。如果我的项目中的任何文件有帮助,我会提供它们。
修复待定:https://github.com/evonove/django-oauth-toolkit/pull/341
当请求的字符编码与 django.conf.settings.DEFAULT_CHARSET
相同时,看起来会发生这种情况,这将导致 request.encoding
变为 None
,as documented。
我用 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 框架版本 3.3.2 和 Django OAuth 工具包版本 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
。如果我的项目中的任何文件有帮助,我会提供它们。
修复待定:https://github.com/evonove/django-oauth-toolkit/pull/341
当请求的字符编码与 django.conf.settings.DEFAULT_CHARSET
相同时,看起来会发生这种情况,这将导致 request.encoding
变为 None
,as documented。