“错误”:“ invalid_client” django-oauth-toolkit

时间:2019-01-02 09:06:48

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

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

~/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"
}

我无法理解问题的出处。我已经搜索了很多,却没有找到正确的答案。请告诉我该怎么做才能消除此错误。

2 个答案:

答案 0 :(得分:2)

我找到了解决方案,而不是我使用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"}

OR

如果您想使用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上。

答案 1 :(得分:0)

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

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());

您的客户应用程序授权授予类型应为:“基于资源所有者的密码” oauth client application: Auth grant type

P.S。我无法通过“ Content-Type”:“ application / json” 来获取令牌,不确定为什么(django-oauth-toolkit文档对此一无所知)。