我已经构建了一个 Laravel rest Api,我想将它用于我的 angular 项目。我已经为登录用户构建了一个表单,但是当我提交公式时出现此错误 POST http://localhost:8000/oauth/token 400(错误请求) 我不知道如何解决。 这是我的 authService.ts 文件
export class AuthService {
//variables
authUrl = 'http://localhost:8000/oauth/token';
apiUrl = 'http://localhost:8000/api';
options: any;
constructor(private http: HttpClient) {
this.options = {
headers: new HttpHeaders({
Accept: 'application/json',
'Content-Type': 'application/json'
})
};
}
login(e :string, p: string) {
return this.http.post(this.authUrl, {
grant_type: 'password',
client_id: '2',
client_secret: '9oi905dZlXpGTGdTjB7Yciqs3XpFEHGiqzTqbkU6',
username: e,
password: p,
scope: '*'
}, this.options);
}
/**
* Revoke the authenticated user token
*/
logout(){
this.options.headers.Authorization = 'Bearer ' + localStorage.getItem('access_token');
return this.http.get(this.apiUrl + '/token/revoke', this.options);
}
}
这里是 signin.component.ts
export class SigninComponent implements OnInit {
signInForm: FormGroup;
loading: boolean;
errors: boolean;
constructor(private formBuilder: FormBuilder,
private authService: AuthService,
private router: Router) { }
ngOnInit(): void {
this.initForm();
}
initForm() {
this.signInForm = this.formBuilder.group({
email: ['', [Validators.required, Validators.email]],
password: ['', [Validators.required, Validators.pattern(/[0-9a-zA-Z]{4,}/)]]
})
}
onSubmit() {
this.loading = true;
this.errors = false;
const email = this.signInForm.get('email').value;
const password = this.signInForm.get('password').value;
this.authService.login(email, password).subscribe(
(res: any) => {
localStorage.setItem('acces_token', res.acces_token);
this.loading = false;
console.log("success");
//navigate to the home page
this.router.navigate['/books'];
}, (err: any) => {
this.loading = false;
this.errors = true;
console.log("Merde combi !");
}
);
}
}