1 pièce(s) jointe(s)
POST 401: Invalid access token: nul
Salut,
J'ai fini mon application en angular7, mon api marche correctement avec Postman et même avec le navigateur en utilisant un lien.
Mais si je fais un post avec angular, j'obtiens l'erreur 401.
Merci de m'aider SVP
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
obtainAccess(data) {
let params = new URLSearchParams();
params.append('grant_type', 'password');
params.append('username', data.username);
params.append('password', data.password);
params.append('client_id', 'eddy');
params.append('scope','openid');
const headers = new HttpHeaders(
{
'Content-type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest',
'Authorization': 'Basic '+btoa('eddy:secret')
}
)
let options = {
headers: headers
};
return this._http.post(
this.url
, params.toString()
, {headers}
).subscribe(
data=>this.saveToken(data)
)
//console.log(options);
}
saveToken(token){
var expireDate = new Date().getTime() + (1000 * token.expires_in);
this.Cookie.set("access_token", token.access_token, expireDate);
this.router.navigate(['/']);
} |
Login TS
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
export class LoginComponent implements OnInit {
public data={
username: "",
password: "",
};
constructor(
private auth: AuthentificationService
) {}
ngOnInit() {
}
public login() {
this.auth.obtainAccess(this.data);
}
} |
LOGIN HTML
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
<div class="container spacer">
<div class="card col-12 col-md-6 justify-content-center">
<div class="card-header">Authentification</div>
<div class="card-body">
<form #f="ngForm" (ngSubmit)="login(f.value)">
<div class="form-group">
<label>Email Address</label>
<input type="text" class="form-control" placeholder="name@address.com" [(ngModel)]="data.username" name="username">
</div>
<div class="form-group">
<label>Email Address</label>
<input type="password" class="form-control" placeholder="password" [(ngModel)]="data.password" name="password">
</div>
<button class="btn btn-success" type="submit">valider</button>
</form>
</div>
</div>
</div> |
Page Erreur:
Pièce jointe 492113