遵循该教程后,我运行了Spring应用程序:https://www.youtube.com/watch?v=IOgCMtYMr2Q&list=PLcoE64orFoVsxAam_BuQBrNC8IO238SwH&index=2 一切正常,但是...当我尝试执行POST请求
localhost:8080/oauth/token?grant_type=password&username=user&password=password
邮递员给了我这个问题:
{
"timestamp": "2019-06-27T13:46:33.144+0000",
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/oauth/token"
}
其中一项评论可能是答案 “ Rocketman并没有说(或者我错过了),您需要指定基本身份验证,客户端ID和机密。一旦完成,便可以获取访问令牌。” 但是我不知道该怎么做。
答案 0 :(得分:1)
您需要指定基本身份验证,客户端ID和秘密密码。在这种情况下,客户端ID为 my-trusted-client ,密码为秘密。以下是用于获取代币的完整代码:
login(){
var params = new URLSearchParams();
params.append('grant_type', 'password');
params.append('username', this.username);
params.append('password',this.password);
axios({
method:'post',
url:'oauth/token',
auth:{username:'my-trusted-client',password:'secret'},
headers: {"Content-type": "application/x-www-form-urlencoded; charset=utf-8"},
data:params
}).then(function(response){
set_cookie("access_token",response.data.access_token);
document.location.replace("/");
});
}