我正在学习Django和Django REST框架。我想用oAuth作为autorization系统。 我安装了Django OAuth Toolkit并注册了新的App.Then,我试图为我的应用程序获取令牌,但我无法得到它:
19:04 ~$ 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/
bash: !G=oi5pHtbgxW96I9w8iTd0: event not found
答案 0 :(得分:2)
在最后一个参数周围使用单引号:
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/'
如果您不这样做,那么shell会将!
解释为调用历史记录的运算符。 ;
将被解释为命令分隔符。
答案 1 :(得分:2)
我使用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 :(得分:1)
我试过了:
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'
我在手册(link)
上做了一切答案 3 :(得分:1)
就我而言,下面是工作:
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/
注意我使用了标志' * '。
答案 4 :(得分: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());
P.S。我无法通过“ Content-Type”:“ application / json” 来获取令牌,不确定为什么(django-oauth-toolkit文档对此一无所知)。