带有Angular4的Laravel 5.4:Passport请求令牌

时间:2017-07-26 19:04:10

标签: angular laravel token laravel-passport

我想从API请求Token:

Signin(email: string, password: string): void {
    let data: Object = {
        client_id: 2,
        client_secret: 'vSFxVqALQHjyotPyGfhrGj3ziudUGsts2ZWiAGms',
        grant_type: 'password',
        username: email,
        password: password,
    };

    this._HTTP.post(
        this.OAuth + 'token',
        data,
        {headers: this.Headers}
    ).toPromise()
    .then( (_response) => {
       console.log (_response); 
    });
}

我收到401 Unauthorized

的错误

21:54:03.996错误错误:未捕获(承诺):状态响应:401网址未经授权:http://localhost:8000/oauth/token

我不明白为什么会收到此错误,我在内核中添加了 - >中间件 - >网址:

\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,

1 个答案:

答案 0 :(得分:0)

Yo应该使用内容类型float:left,并提供如下角度服务:

application/x-www-form-urlencoded

在laravel中:

  1. 护照配置(更改:https://github.com/dedd1993/laravel-oauth2-server/commit/da17f500be60958378be898021414383b541961e)。
  2. 添加cors(https://github.com/barryvdh/laravel-cors),此处为示例https://github.com/dedd1993/laravel-oauth2-server/commit/e0b942cd3674fd0464cdbb3572cf7a8bd7849755
  3. 最后,将APIS分组到身份验证中间件中,此处为示例https://github.com/dedd1993/laravel-oauth2-server/commit/5b05538e2d102e00949edc667b85df102f770186

我有两个例子

  

完整的角形对象:https://github.com/dedd1993/ngx-admin

     

Laravel基本oauth:https://github.com/dedd1993/laravel-oauth2-server