【发布时间】:2017-09-24 08:36:28
【问题描述】:
我正在使用 django 的 rest_framework 库为 django 应用程序构建一个 API。一切都很顺利,我可以按预期通过 curl 命令访问我的 API。
现在,我想以使用 CoreAPI 的客户端库的形式让事情变得更加优雅。
我可以进行如下基本身份验证:
auth = coreapi.auth.BasicAuthentication(username=user, password=password)
client = coreapi.Client(auth=auth)
而且我能够很好地访问 API 的架构。
但是,我想使用我的令牌身份验证(通过 rest_framework.tokenauthenticaiton)(通过 curl 可以正常工作)我收到一个错误,我的代码如下所示:
token = 'Token abc12345'
#tried the following:
#token = 'abc12345'
#token = 'Authorization: Token abc12345'
auth = coreapi.auth.TokenAuthentication(token=token)
client = coreapi.Client(auth=auth)
尝试访问架构,我得到:
coreapi.exceptions.ErrorMessage: <Error: 401 UNAUTHORIZED>
detail: "Authentication credentials were not provided."
文档显示 TokenAuthentication 需要架构和令牌作为参数,但是示例显示 TokenAuthentication 使用 JWT,而不是 djangos rest_framework.tokenauthentication。
任何建议将不胜感激!
【问题讨论】:
标签: django-rest-framework core-api