-
Notifications
You must be signed in to change notification settings - Fork 815
Description
I performed the first 3 part (https://django-oauth-toolkit.readthedocs.org/en/0.7.0/tutorial/tutorial.html) of the tutorial and I'm now trying to perform the "Django Rest Framework" (https://django-oauth-toolkit.readthedocs.org/en/0.7.0/rest-framework/getting_started.html) but that part cannot be completed as it is due to curl issue.
At step 4 of this part of the tutorial (https://django-oauth-toolkit.readthedocs.org/en/0.7.0/rest-framework/getting_started.html#step-4-get-your-token-and-use-your-api), it says:
At this point we’re ready to request an access_token. Open your shell
curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" http://<client_id>:<client_secret>@localhost:8000/o/token/
The problem here is that "client_id" and "client_secret" aren't alpha-numeric string.
In my case, I got:
client_id: .EW6rNN0pkmfDWFaM.vtlCuMbD-W6RgRJjeASrVK
client_secret: ?LZSZzO5SNWm?ul5!9XGyLFqfI2AZ@??Y_UXWXTnfQ3y;!Q@B2jA3k1YX7QFLPk8Iyiy95ZRUfutlfIpoEnB!@Diu3gzk8sSy6t:gW0lMbL!@DxJoBfyRcFXzeYiQlHo
I tried and tried, read bunch of curl documentation, but there doesn't seem to be a way to pass such weird string with curl. Special characters (e.g. ! ? ;) will be interepreted by the shell so need to be escaped... but even though, escaped characters and fake "control" characters (e.g. @ : / ) will confuse the hell out of curl.
I'm not the typical web developer type, I was able to work my way around (see work around below) using perl to URI encode the strings. But this was super frustrating, took me 2 hours and I doubt any beginner web developer would ever be able to do that.
For the record:
OS: Linux CentOS 6.5
curl version: curl 7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Shell (bash) version: GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Various tests details
If I try to pass it straight to curl (as seen in the tutorial) I get:
[wbourque@localhost ~]$ curl -X POST -d "grant_type=password&username=wibou7&password=123456" http://.EW6rNN0pkmfDWFaM.vtlCuMbD-W6RgRJjeASrVK:?LZSZzO5SNWm?ul5!9XGyLFqfI2AZ@??Y_UXWXTnfQ3y;!Q@B2jA3k1YX7QFLPk8Iyiy95ZRUfutlfIpoEnB!@Diu3gzk8sSy6t:gW0lMbL!@DxJoBfyRcFXzeYiQlHo@127.0.0.1:8000/o/token/
bash: !Q@B2jA3k1YX7QFLPk8Iyiy95ZRUfutlfIpoEnB!@Diu3gzk8sSy6t: event not found
Curl documentation suggest putting the URL into double quote (i.e. " ") when the link contains special characters... However, doing so here won't do us much good:
[wbourque@localhost ~]$ curl -X POST -d "grant_type=password&username=wibou7&password=123456" "http://.EW6rNN0pkmfDWFaM.vtlCuMbD-W6RgRJjeASrVK:?LZSZzO5SNWm?ul5!9XGyLFqfI2AZ@??Y_UXWXTnfQ3y;!Q@B2jA3k1YX7QFLPk8Iyiy95ZRUfutlfIpoEnB!@Diu3gzk8sSy6t:gW0lMbL!@DxJoBfyRcFXzeYiQlHo@127.0.0.1:8000/o/token/"
bash: !9: event not found
Obviously, the ! characters (and all the others special characters) is interpreted by the shell so it needs to be escaped.
If I try just that:
[wbourque@localhost ~]$ curl -X POST -d "grant_type=password&username=wibou7&password=123456" http://.EW6rNN0pkmfDWFaM.vtlCuMbD-W6RgRJjeASrVK:\?LZSZzO5SNWm\?ul5\!9XGyLFqfI2AZ\@\?\?Y_UXWXTnfQ3y\;\!Q\@B2jA3k1YX7QFLPk8Iyiy95ZRUfutlfIpoEnB\!\@Diu3gzk8sSy6t\:gW0lMbL\!\@DxJoBfyRcFXzeYiQlHo@127.0.0.1:8000/o/token/
curl: (6) Couldn't resolve host ''
Ok, host is empty, that's weird..?
Let's try to put all that into double quote, maybe?
[wbourque@localhost ~]$ curl -X POST -d "grant_type=password&username=wbourque&password=123456" "http://.EW6rNN0pkmfDWFaM.vtlCuMbD-W6RgRJjeASrVK:?LZSZzO5SNWm\?ul5\!9XGyLFqfI2AZ\@\?\?Y_UXWXTnfQ3y\;\!Q\@B2jA3k1YX7QFLPk8Iyiy95ZRUfutlfIpoEnB\!\@Diu3gzk8sSy6t\:gW0lMbL\!\@DxJoBfyRcFXzeYiQlHo@172.16.180.204:8000/o/token/"
curl: (6) Couldn't resolve host '\'
Ok... obviously curl is just super confused with anything I send him here.
Work around
Ok, what if I use another language (e.g. perl) to URI Encode that part of the URL?
Let's try:
#!/bin/bash
CLIENT=".EW6rNN0pkmfDWFaM.vtlCuMbD-W6RgRJjeASrVK"
SECRET="?LZSZzO5SNWm?ul5!9XGyLFqfI2AZ@??Y_UXWXTnfQ3y;!Q@B2jA3k1YX7QFLPk8Iyiy95ZRUfutlfIpoEnB!@Diu3gzk8sSy6t:gW0lMbL!@DxJoBfyRcFXzeYiQlHo"
value_client="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$CLIENT")"
value_secret="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$SECRET")"
curl -X POST -d "grant_type=password&username=wibou7&password=123456" "http://$value_client:$value_secret@127.0.0.1:8000/o/token/"
Now when I execute that, I get:
[wbourque@localhost ~]$ ./bug_that.sh
{"access_token": "j71tyLppbX2kjebvtCH34g2tYzsDms", "token_type": "Bearer", "expires_in": 36000, "refresh_token": "JEfVGq30uaPIFmRhl8bh8yth6cYdPq", "scope": "read write groups"}
IT WORKS!!!
So in conclusion:
This will NEVER work using shell only... The only way seems to use something else to URI encode before calling curl.