【问题标题】:django oauth toolkit unsupported_grant_type errordjango oauth 工具包 unsupported_grant_type 错误
【发布时间】:2020-06-26 16:17:43
【问题描述】:

我尝试将 outh2 添加到我的 django 应用程序中,所以我使用了 django oauth 工具包。 所以我按照教程进行操作,但是如果我尝试获取用户令牌,它总是会向我发送 unsupported_grant_type 错误。我该如何解决这个错误?

settings.py

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
    )
}

OAUTH2_PROVIDER = {
    # parses OAuth2 data from application/json requests
    'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.JSONOAuthLibCore',
}

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('myapp.api.urls')),
    path('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
]

客户类型:机密

授权类型:基于资源所有者密码

chrome advanced rest client

网址:http://client_id:client_secret@localhost:8000/o/token/

requirements.txt

asgiref==3.2.5
autopep8==1.5
certifi==2019.11.28
chardet==3.0.4
Django==3.0.4
django-oauth-toolkit==1.3.0
djangorestframework==3.11.0
idna==2.9
oauthlib==3.1.0
pycodestyle==2.5.0
pytz==2019.3
requests==2.23.0
sqlparse==0.3.1
urllib3==1.25.8

【问题讨论】:

    标签: django oauth


    【解决方案1】:

    只需删除 OAUTH2_PROVIDER 设置中的 OAUTH2_BACKEND_CLASS

    OAUTH2_PROVIDER = {
        # parses OAuth2 data from application/json requests
        # 'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.JSONOAuthLibCore',
        # this is the list of available scopes
        'SCOPES': {'read': 'Read scope', 'write': 'Write scope', 'groups': 'Access to your groups'}
    }
    

    如果您打算使用 OAUTH2_BACKEND_CLASS,您应该以 JSON 格式发送正文。

    {
        "grant_type":"password",
        "client_id":"<client_id>",
        "client_secret":"<client_secret>",
        "username":"<usename>",
        "password":"<password>"
    }
    
    curl -X POST -d '{"grant_type":"password","client_id":"<client_id>","client_secret":"<client_secret>","username":"<username>","password":"<password>"}' http://localhost:8000/o/token/
    

    【讨论】:

      【解决方案2】:

      1.你必须创建应用程序:

      http://localhost:8000/o/applications/
      

      点击链接创建一个新应用程序并在表格中填写以下数据:

      名称:只是您选择的名称

      客户类型:保密

      授权授予类型:基于资源所有者密码 你给 clientIdSecretClient

      2。获取您的令牌并使用您的 API

      curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
      

      示例:

      curl -X POST -d "grant_type=password&username=Alex&password=Won123" -u"cWS5WudFiBhHh6BZxcaOgRGfrZjhcP2rfQcWVyaU:puNJ1SgQbp39Ai1TmYJx0wL9LnC6FNarnY3dgYBA3Z7AgQR5zRcfw5U144zxJ6vndW0jtV4WWKip33kQnFUl4v73xt2IosLGHo7k1w35R7aK8aFL3ZBoMcQ3pyaWjkBT" http://127.0.0.1:8000/o/token/
      

      用户名和密码是在您的授权服务器中注册的用户的凭据,最后作为响应,您提供令牌并使用这样的令牌来处理您的请求:

      curl -H "Authorization: Bearer <your_access_token>" http://localhost:8000/api/users
      

      注意您的应用程序配置以及您的用户名和密码。

      【讨论】:

      猜你喜欢
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2014-04-02
      • 2019-04-16
      • 2018-08-08
      • 1970-01-01
      相关资源
      最近更新 更多