0

I want use oauth2 in my ionic app but i get error:

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

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

Response:

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

Angular function:

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);  }

All params are correct.

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

return

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

I can get token using PostMan and curl, CORS in my server is enabled for this endpoint so i think the auth server works properly.

Does anyone know what I'm doing wrong?

2 Answers2

0

Try to add this headers.set('Authentication', 'Basic client_id:client_secret'); (in Base 64)

0

The problem was in dependency. Most likely HTTPClient((@angular/common/http) from angular 4.3.4 desn't work with ionic for this moment. With Http (@angular/http) from angular 4.1.3 above code works properly.