在CoreAPI中使用TokenAuthentication时遇到问题

rob*_*rob 4 core-api django-rest-framework

我正在使用django的rest_framework库为django应用程序构建API.一切都很顺利,我可以通过curl命令按预期访问我的API.

现在,我想使用CoreAPI以客户端库的形式使事情变得更加优雅.

我可以进行如下基本身份验证:

auth = coreapi.auth.BasicAuthentication(username=user, password=password)
client = coreapi.Client(auth=auth)
Run Code Online (Sandbox Code Playgroud)

我能够很好地访问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)
Run Code Online (Sandbox Code Playgroud)

试图访问架构,我得到:

coreapi.exceptions.ErrorMessage: <Error: 401 UNAUTHORIZED>
    detail: "Authentication credentials were not provided."
Run Code Online (Sandbox Code Playgroud)

文档显示TokenAuthentication需要schema和token作为参数,但是该示例显示了使用JWT的TokenAuthentication,而不是djangos rest_framework.tokenauthentication.

任何意见,将不胜感激!

小智 6

我刚才遇到了同样的问题.修复是为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)
Run Code Online (Sandbox Code Playgroud)