angular - 如何使用正确的标头使用 Angular 6 发送 OAuth2 POST 请求?
问题描述
我需要向 Spring Boot OAuth 2 授权服务器发送发布请求以获取 JWT 令牌?使用 Postman 我得到了令牌并且 Auth Server 工作正常。但是对于 Angular,我很难找出正确的发布请求格式?
这是我正在使用的当前方法。
login() {
const url = 'http://localhost:9090/oauth/token';
const data = {
'grant_type': 'password',
'username': 'username',
'password': 'password'
};
const reqH = new HttpHeaders({
'Content-Type': 'application/x-www-urlencoded',
'Authorization': 'Basic ' + btoa('client-id' + ':' + 'secret'),
'grant_type': 'password',
'username': 'administrator%40divinedragon.com',
'password': 'password'
});
return this.http.post('http://localhost:9090/oauth/token', data, {
headers: reqH
});
}
使用此方法时,出现以下错误。
HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "OK", url: "http://localhost:9090/oauth/token", ok: false, ...},
错误:{错误:“无效请求”,错误描述:“缺少授权类型”}
非常感谢您的帮助!
解决方案
我认为您也将用户名和密码放在标题和正文中。它对我有用,如下所示:
login(username, password) {
const headers = {
'Authorization': 'Basic ' + btoa('devglan-client:$2a$04$e/c1/RfsWuThaWFCrcCuJeoyvwCV0URN/6Pn9ZFlrtIWaU/vj/BfG'),
'Content-type': 'application/x-www-form-urlencoded'
}
const body = new HttpParams()
.set('username', username)
.set('password', password)
.set('grant_type', 'password');
this.http.post('http://localhost:8080/' + 'oauth/token', body, {headers})
.subscribe(data => this.setToken(data),
err => alert('invalid Creadtilas'));
}
推荐阅读
- php - 请告诉这个mysql查询中的错误
- vue.js - Jest 使用 Ionic Capacitor 插件导入测试 Vue 单文件组件
- python - 如果包装在 for 函数中,函数无法转换列表元素
- excel - 在函数中使用 IsNumeric
- google-geocoding-api - 通过php映射googleapis com中的错误邮政编码
- amazon-web-services - 带有对象标签的 AWS S3 访问策略
- html - 使用 Excel VBA 更改 HTML 属性
- actions-on-google - 谷歌上的操作未更新评级
- javascript - 如何知道带有接收 204 无内容作为响应的 url 的图像是否加载
- visual-studio-2017 - 如何为 VS 2017 安装 WiX 扩展