2017-08-17 57 views
0

我想在我的離子應用程序中使用的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:// stackoverflow。 com/questions/45751648/angular4-doesnt-send-post-data –

回答

0

的問題是在相關性。最有可能來自角4.3.4的HTTPClient((angular/common/http)目前無法與離子一起使用。從上述代碼的角4.1.3開始的Http(@ angular/http)

0

嘗試添加此 headers.set('Authentication', 'Basic client_id:client_secret');(在基礎64)

+0

我的grant_type正確。 –

+0

您的角度請求如何? –

+0

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