角HttpPost到OAuth2返回401

问题描述:

我想在我的离子应用程序中使用的oauth2,但我得到的错误:角HttpPost到OAuth2返回401

Failed to load resource: the server responded with a status of 401()

Error: Http failure response for http://localhost:8080/oauth/token : 401 OK

响应:

{"timestamp":1502983544253,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/oauth/token"}

角功能:

login(username, password) : Observable<any> { 

let params: HttpParams = new HttpParams(); 
params.set('username', username); 
params.set('password', password); 
params.set('client_id', this.clientId); 
params.set('client_secret', this.clientSecret); 
params.set('grant_type', 'password'); 


let headers = new HttpHeaders(); 
headers.set('Content-Type', 'application/x-www-form-urlencoded'); 

return this.http.post(this.EndPointUrl, { 
    params:params 
}, { headers:headers}).map(this.handleData). 
    catch(this.handleError); } 

所有PARAMS是正确的。

curl -X POST --user 'U:P' -d 'grant_type=password&username=USER&password=PASS' localhost:8080/oauth/toke 

回报

{"access_token":"d88c1c5e-d2c0-4c96-9e69-a62816d0a5e8","token_type":"bearer","refresh_token":"8f82f0b0-38a3-4c05-9590-43d3ec2f36b9","expires_in":3599,"scope":"read write"} 

我可以用邮递员和卷曲获得令牌,CORS在我的服务器此终结点启用,所以我觉得auth服务器工作正常。

有谁知道我在做什么错?

+0

是吗?而不是你的选择请求失败? –

+0

我不这么认为,看看https://i.stack.imgur.com/scmnI.png。 我认为角度不发送数据在后。https:// *。 com/questions/45751648/angular4-doesnt-send-post-data –

的问题是在相关性。最有可能来自角4.3.4的HTTPClient((angular/common/http)目前无法与离子一起使用。从上述代码的角4.1.3开始的Http(@ angular/http)

尝试添加此 headers.set('Authentication', 'Basic client_id:client_secret');(在基础64)

+0

我的grant_type正确。 –

+0

您的角度请求如何? –

+0

也许您可以更改为headers.set('Content-Type','application/json') ; –