gpt4 book ai didi

django-rest-framework - 在 CoreAPI 中使用 TokenAuthentication 时遇到问题

转载 作者:行者123 更新时间:2023-12-05 00:15:44 26 4
gpt4 key购买 nike

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

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

我能够进行如下基本身份验证:

auth = coreapi.auth.BasicAuthentication(username=user, password=password)
client = coreapi.Client(auth=auth)

而且我能够很好地访问 API 的架构。

但是,我想使用我的 token 身份验证(通过 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 需要架构和 token 作为参数,但是示例显示了使用 JWT 的 TokenAuthentication,而不是使用 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 - 在 CoreAPI 中使用 TokenAuthentication 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43642702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com