1

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.

Aadil Shaikh
  • 311
  • 1
  • 3
  • 19

2 Answers2

5

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.

Aadil Shaikh
  • 311
  • 1
  • 3
  • 19
0

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" oauth client application: Auth grant type

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

Dmytro Gierman
  • 152
  • 1
  • 6