我正在使用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。
如有任何建议,将不胜感激!
发布于 2017-05-23 03:18:47
我只是有同样的问题。修复方法是为coreapi.auth.TokenAuthentication设置一个“Token=‘Token’”参数。所以,像这样的东西可能对你有用:
token = 'abc12345' # don't put the word 'Token' in front.
auth = coreapi.auth.TokenAuthentication(scheme='Token', token=token)
client = coreapi.Client(auth=auth)https://stackoverflow.com/questions/43642702
复制相似问题