【问题标题】:"error": "invalid_client" django-oauth-toolkit“错误”:“invalid_client”django-oauth-toolkit
【发布时间】:2019-05-14 15:37:11
【问题描述】:

我正在使用带有 django-oauth-toolkit 的 django rest 框架。当我在本地主机上请求访问令牌时,它会为我提供如下所示的访问令牌

~/django_app$ curl -X POST -d "grant_type=password&username=<Your-username>&password=<your-password>" -u"<client-id>:<client-secret>" http://localhost:8000/o/token/
{"access_token": "8u92BMmeZxvto244CE0eNHdLYWhWSa", "expires_in": 36000, "refresh_token": "faW06KKK71ZN74bx32KchMXGn8yjpV", "scope": "read write", "token_type": "Bearer"}

但是当我从实时服务器上托管的同一个项目请求访问令牌时,它给我的错误是 invalid_client。

~/django_app$ curl -X POST -d "grant_type=password&username=<Your-username>&password=<your-password>" -u"<client-id>:<client-secret>" http://<your-domain>/o/token/ 
{
    "error": "invalid_client"
}

我无法理解问题出在哪里。我搜索了很多并没有找到正确的答案。请告诉我该怎么做才能摆脱这个错误。

【问题讨论】:

  • 您是否在实时服务器上注册了您的 oauth 应用程序并获得了客户端 ID 和客户端密码?
  • 是的,我已经注册了

标签: django django-rest-framework django-oauth


【解决方案1】:

我找到了解决方案,而不是grant_type=password,我使用了grant_type=client_credentials,然后我得到了访问令牌。你可以在下面看到 curl 命令。

curl -X POST -d "grant_type=client_credentials&client_id=<your-client id>client_secret=<your-client secret>" http://your-domain/o/token/
{"scope": "read write", "token_type": "Bearer", "expires_in": 36000, "access_token": "ITx5KCjupsdbvbKvNQFyqZDEw6svSHSfdgjh"}

如果你想用grant-type=password 来做这件事,那么这里是命令:

curl -X POST -d "grant_type=password&username=<your-username>&password=<your-password>&client_id=<your-client id>&client_secret=<your-client secret>" http://your-domain/o/token/
{"access_token": "0BVfgujhdglxC7OHFh0we7gprlfr1Xk", "scope": "read write", "token_type": "Bearer", "expires_in": 36000, "refresh_token": "AwffMPzNXvghlkjhs8dpXk7gbhhjhljlldfE2nI"}

我引用了这个https://developer.amazon.com/de/docs/adm/request-access-token.html,因为我的应用程序在 AWS 上。

【讨论】:

    【解决方案2】:

    在 JavaScript 中从 django-oauth-toolkit 获取令牌:

    async function getToken () {
        let res = await fetch("https://<your_domain>/o/token/", {
            body: new URLSearchParams({
                grant_type: 'password',
                username: '<user_name>',
                password: '<user_pass>',
                client_id: '<client_app_id>',
                client_secret: '<client_pass>'
            }),
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: "POST"
        })
        return res.json();
    }
    console.log(await getToken());
    

    您的客户端应用程序授权类型应为:“基于资源所有者密码”

    附:我无法通过 "Content-Type": "application/json" 获取令牌,不知道为什么(django-oauth-toolkit 文档对此只字未提)。

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2016-02-25
      • 2020-08-14
      • 1970-01-01
      • 2020-05-15
      相关资源
      最近更新 更多