I'm doing oauth2 with angular 2.
Here is the POST URL = "http://localhost:8080/OAuth2Example/oauth/token?grant_type=password&username=bill&password=abc123".
how to pass clientId and client secret in frontend with one of validated user and get access token?
If any plugin is available in angular2 or any configuration please suggest me!
I tried like,
private getHeaders(){
// I included these headers because otherwise FireFox
// will request text/html
let headers = new Headers();
let auth_data = 'username=my-trusted-client&password=secret';
headers.append('Accept', 'application/json');
headers.append('Authorization', auth_data);
return headers;
}
testRequest() {
let data = 'grant_type=password&username=bill&password=abc123';
let postResponse = this.http
.post(`${this.baseUrl}/oauth/token?`, data, {headers:
this.getHeaders()})
.toPromise()
.then(this.mapResult);
return postResponse;
}
When i execute Post call,
XMLHttpRequest cannot load http://localhost:8080/OAuth2Example/oauth/token?/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 401.
this error I saw at console.
I have a postman collection snaps to quickly identify what i need exactly, I achieved oauth2 from postman like below.
This is exactly i need to get it from angular 2 too.
Thanks,


http://localhost:8080/SpringSecurityOAuth2Example/oauth/token?/the literal path the actually shows? Because thetoken?/part looks like a mistake. It also doesn’t at all match thehttp.getrequest code shown in the question. And that request code doesn’t match the “POST URL” shown at the beginning of the question. So it’s hard to try to figure out what might be going on here, but regardless, if the server you’re sending that request to responds to the OPTIONS request for that path with a 401, then you’re not going to be able to get the browser to ever even try that POST request - sideshowbarker.post(`${this.baseUrl}/oauth/token`, data, {headers: this.getHeaders()}). That is, remove the?from the end of the request URL. - sideshowbarker