python – 尝试请求OAuth access_token时我得到:TypeError:encode()参数1必须是字符串,而不是无

我在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 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)
    [...]

但不知道为什么编码值是None.如果我项目中的任何文件都有用,我会提供.

最佳答案 修复正在等待:
https://github.com/evonove/django-oauth-toolkit/pull/341

当请求中的char编码与django.conf.settings.DEFAULT_CHARSET相同时,会发生这种情况,这会导致request.encoding为None,as documented.

点赞