【发布时间】:2017-06-22 03:27:22
【问题描述】:
我正在尝试在 angularjs 中发出请求以检索访问令牌,但我不是 100% 确定如何去做。这是 curl 中的请求
curl -X POST -d “grant_type=密码&用户名=&密码=&范围=读取” -u":" http://localhost:8000/o/token/
感谢所有帮助
【问题讨论】:
我正在尝试在 angularjs 中发出请求以检索访问令牌,但我不是 100% 确定如何去做。这是 curl 中的请求
curl -X POST -d “grant_type=密码&用户名=&密码=&范围=读取” -u":" http://localhost:8000/o/token/
感谢所有帮助
【问题讨论】:
我认为您需要使用查询字符串参数发布
为此,您需要在您的发布请求中进行一些修改
var request = $http({
method: "POST",
url: "",
transformRequest: transformRequestAsFormPost,
data: {
grant_type: "password",
username: "Kim",
password: "123",
scope: "read"
}
});
这里有更详细的解释https://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm
【讨论】:
我想通了
var data = "grant_type=password" + "&username="+cred.username + "&password="+cred.password +
"&client_id=" + cred.client_id +
"&client_secret=" + cred.client_secret;
$http({
method: 'POST',
url: 'BaseUrl',
data: data,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
【讨论】: