“error”: “invalid_client” django-oauth-toolkit

对着背影说爱祢 提交于 2020-08-11 04:19:21

问题


I am using django rest framework with django-oauth-toolkit. When i request access token on my localhost it gives me the access token as shown below

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

But when i request the access token from the same project hosted on live server, it give me error as 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"
}

I am not able to understand where is the problem coming from. I have searched a lot and didn't find the right answer. Please advise me what to do to get rid of this error.


回答1:


I found the solution for this, instead of grant_type=password i have used grant_type=client_credentials then i got the access token. You can see the curl command below.

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

If you want to do it with grant-type=password then here is command for that:

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

I referred this https://developer.amazon.com/de/docs/adm/request-access-token.html as my application was on AWS.




回答2:


Get token from django-oauth-toolkit in 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());

Your client application authorisation grant type should be: "Resource owner password-based"

P.S. I've failed to get token via "Content-Type": "application/json", not sure why (django-oauth-toolkit documentation says nothing about that).



来源:https://stackoverflow.com/questions/54003651/error-invalid-client-django-oauth-toolkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!