【问题标题】:django-oauth2-provider: get the token without sending the client_secretdjango-oauth2-provider:获取令牌而不发送 client_secret
【发布时间】:2014-04-25 19:22:39
【问题描述】:

我正在使用 django-oauth2-provider 和 rest-framework 在我的 API 中提供身份验证。 我的移动应用程序将连接到我的 REST API 以检索一些数据。没有第三方应用程序会参与此过程。

根据this,此用例所需的授权类型将是密码授权。由于将密钥存储在设备中是个坏主意,因此我需要在没有它的情况下访问令牌。

我尝试发送不带密码的请求:

curl -X POST -d "client_id=MY_CLIENT_ID&grant_type=password&username=user&password=pass" http://localhost:8000/oauth2/access_token/

但我得到的回应是:

{"error": "invalid_client"}

我的问题是是否可以使用 django-oauth2-provider 来做到这一点,以及如何做到这一点。

【问题讨论】:

    标签: python django oauth-2.0


    【解决方案1】:
    1. Authorization Grant Type 设置为Resource owner password-based
    2. WSGIPassAuthorization OnWSGIScriptAlias 放在同一位置

    【讨论】:

      【解决方案2】:

      您需要通过 django admin UI 创建客户端,并将“MY_CLIENT_ID”替换为 ID。

      【讨论】:

      • 我已经这样做了。问题是,如果不发送客户端密码,我就无法获取令牌数据。我总是收到 invalid_client 错误。所以我需要的是一种在不发送客户端密码的情况下获取令牌的方法。
      【解决方案3】:

      应用程序的“客户端类型”应为“公共”

      【讨论】:

        【解决方案4】:

        只是结合解决方案。这对我有用。关注Getting Started guide。但是,在创建应用程序时,请提供以下内容:

        • 名称:您选择的名称
        • 客户端类型:公共
        • 授权授予类型:基于资源所有者密码

        那么请求应该是:

        curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=password&username=<username>&password=<password>&client_id=<client_id>" http://localhost:8000/o/token/
        

        或者,如果是 JSON, 在 settings.py 中添加:

        OAUTH2_PROVIDER = {
            # expect request body Content Type application/json
            'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.JSONOAuthLibCore'
        }
        
        curl -X POST \
          http://localhost:8000/o/token/ \
          -H 'Content-Type: application/json' \
          -d '{
            "grant_type": "password",
            "client_id": "<client_id>",
            "username": "<username>",
            "password": "<password>"
        }'
        

        【讨论】:

          【解决方案5】:

          您应该使用密码授权类型。以下 curl 命令适用于 django-oauth-toolkit。我相信它也应该与任何其他 oauth 提供商一起使用。

          curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=password&username=user&password=pass&client_id=client_id' 'http://localhost:8000/o/token/'
          

          更多信息请查看以下链接:https://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified#password

          【讨论】:

            猜你喜欢
            • 2020-03-08
            • 2012-04-24
            • 1970-01-01
            • 2015-08-22
            • 1970-01-01
            • 2016-10-13
            • 2017-04-04
            • 2012-06-29
            • 2015-04-09
            相关资源
            最近更新 更多