[Django]-Unable to use curl to get a token with Django OAuth Toolkit

3👍

Use single quotes around your last parameter:

curl -X POST -d "grant_type=password&username=admin&password=123" 'http://tygkDGuF.re@HmpJB!G=oi5pHtbgxW96I9w8iTd0:zFpyRXwgUKglYAs6b=v8UwlA3qqxZr!=hn3PgzQQ5CgO=OVR-O26reO;T8xcn;xcurcbC7kPr8r-York9TIEKKEM6EFRUOhR:oU!=S3A;QQ1pmAK8dNso48KfYNgYO_F@http://127.0.0.1:8080/o/token/'

If you don’t then the shell will interpret ! as an operator for recalling history. And ; will be interpreted as command separators.

👤Louis

3👍

I used http://docs.python-requests.org/en/latest/:

response = requests.post(
    'http://localhost:8000/o/token/',
    data={
        'grant_type': 'password',
        'username': 'you',
        'password': 'secret',
        'client_id': 'WpDs.u9yrWD3js;fYq?cog;MvTiq0Bj02r8LTL_v',
        'client_secret': 'c1W2:PzVsccsIt_G_uxnwE_TC08z14IKqYYKy0DJAok;_B?RvuVashIsARqhGwF=ChoBJveA7LvB;C?IeXyp?0ZiyBtg9;tSwTjVdC.K_f@n=K;@V;2:VoX@IhPyiHzC'
    }
)

print(response.text)

2👍

I tried this:

curl -X POST -d "grant_type=password&username=name&password=123" 'http://wHMBrLRjQU_!;pYIPgLGurD2SIYH.9r19!h7JSds:wCQ?KHaoVb;aS:-sqCVkkNwrwGP3B?hD9yZTGY;zFCvbR5H8Fy=mh79KYFQBtIJWza380yFF!OHeb-HyFyGtSY54oUK;_qPF4nsaAI0mt_INr.JQ0vdUgAluWPI9EeDk@127.0.0.1:8000/o/token/'
curl: (6) Couldn't resolve host 'wHMBrLRjQU_!;pYIPgLGurD2SIYH.9r19!h7JSds'

I did everything on a manual (link)

1👍

In my case, below is work:

curl -X POST -d 'client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=password&username=YOUR_USERNAME&password=YOUR_PASSWORD' http://localhost:8000/o/token/

Note i used signs ‘*‘.

0👍

And a Javascript solution to get token from 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());

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).

Leave a comment