在 CoreAPI 中使用 TokenAuthentication 时遇到问题
Having trouble using TokenAuthentication in CoreAPI
我正在使用 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。
如有任何建议,我们将不胜感激!
我也遇到了同样的问题。解决方法是为 coreapi.auth.TokenAuthentication 设置一个 "scheme='Token'" 参数。所以,这样的事情可能对你有用:
token = 'abc12345' # don't put the word 'Token' in front.
auth = coreapi.auth.TokenAuthentication(scheme='Token', token=token)
client = coreapi.Client(auth=auth)
我正在使用 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。
如有任何建议,我们将不胜感激!
我也遇到了同样的问题。解决方法是为 coreapi.auth.TokenAuthentication 设置一个 "scheme='Token'" 参数。所以,这样的事情可能对你有用:
token = 'abc12345' # don't put the word 'Token' in front.
auth = coreapi.auth.TokenAuthentication(scheme='Token', token=token)
client = coreapi.Client(auth=auth)