【问题标题】:Tastypie ApiKey Authentication not working for foreman/herokyTastypie Api Key Authentication 不适用于工头/heroku
【发布时间】:2014-09-01 07:10:55
【问题描述】:

我有一个需要 ApiKey 身份验证的 api。如果我在本地使用 manage.py runserver 运行这个 api,它工作得很好,但是如果我在本地使用工头启动并部署到 heroku,它会给出以下错误

{
"error_message": "'NoneType' object has no attribute 'DoesNotExist'", 
"traceback": "Traceback (most recent call last):\n\n  File \"/Users/carlosbalderas/.virtualenvs/recommenu/lib/python2.7/site-packages/tastypie/resources.py\", line 195, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"/Users/carlosbalderas/.virtualenvs/recommenu/lib/python2.7/site-packages/tastypie/resources.py\", line 435, in dispatch_detail\n    return self.dispatch('detail', request, **kwargs)\n\n  File \"/Users/carlosbalderas/.virtualenvs/recommenu/lib/python2.7/site-packages/tastypie/resources.py\", line 453, in dispatch\n    self.is_authenticated(request)\n\n  File \"/Users/carlosbalderas/.virtualenvs/recommenu/lib/python2.7/site-packages/tastypie/resources.py\", line 536, in is_authenticated\n    auth_result = self._meta.authentication.is_authenticated(request)\n\n  File \"/Users/carlosbalderas/.virtualenvs/recommenu/lib/python2.7/site-packages/tastypie/authentication.py\", line 197, in is_authenticated\n    except (User.DoesNotExist, User.MultipleObjectsReturned):\n\nAttributeError: 'NoneType' object has no attribute 'DoesNotExist'\n"

我正在使用这些 curl 与我的 api 交互 (运行服务器)

curl -H "Authorization: ApiKey USERNAME:APIKEY" http://127.0.0.1:8000/api/v1/user/

(工头)

curl -H "Authorization: ApiKey USERNAME:APIKEY" http://0.0.0.0:5000/api/v1/user/

(heroku)

curl -H "Authorization: ApiKey USERNAME:APIKEY" http://fancy-name.herokuapp.com/api/v1/user/

提前感谢您的帮助!

【问题讨论】:

  • 在 shell 中签入时,用户就在那里。每种情况都使用相同的数据库。
  • 嗨 Carlos Balderas,您找到解决方案了吗?我也有同样的问题。它在机器 A 上工作,但在机器 B 上使用相同版本的 django、python 和 sweetpie 失败
  • 我没有,我已经切换到 Django Rest Framework

标签: django api curl heroku tastypie


【解决方案1】:

我知道这是一篇稍旧的帖子,但我刚刚找到了解决方案(无论如何对我来说)。

解决方案是将 API 密钥信号 (described here) 移动到我的应用程序主目录中的 signals.py 文件中。

问题是 Tastypie 通过 User.USERNAME_FIELD 获取用户名字段。如果您创建了自定义用户,或者由于某种原因 django.contrib.admin.get_user_model 方法没有正确返回,那么美味派将默认使用“无”的用户名字段。

这是代码(来自tastepie的compat.py):

    try:
        from django.contrib.auth import get_user_model
        User = get_user_model()
        username_field = User.USERNAME_FIELD
    except ImproperlyConfigured:
        # The the users model might not be read yet.
        # This can happen is when setting up the create_api_key signal, in your
        # custom user module.
        User = None
        username_field = None

由于我在我的 models.py 文件中设置了 api 密钥信号,get_user_model 方法没有正确返回,并且 sweetpie 默认返回 username_field = None。

【讨论】:

    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 2022-09-30
    • 2021-08-28
    • 2021-06-03
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2019-03-24
    相关资源
    最近更新 更多