【问题标题】:Having trouble using TokenAuthentication in CoreAPI在 CoreAPI 中使用 TokenAuthentication 时遇到问题
【发布时间】: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


    【解决方案1】:

    我也遇到了同样的问题。解决方法是为 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)
    

    【讨论】:

      猜你喜欢
      • 2018-01-27
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多