Laravel 使用有效令牌的未授权响应

Laravel 使用有效令牌的未授权响应,laravel,rest,api,ionic-framework,ionic3,Laravel,Rest,Api,Ionic Framework,Ionic3,成功登录后,我在每个请求上都会收到未经授权的响应 这是我的一些代码(如果您需要查看其他内容,请告诉我): 爱奥尼亚的数据提供者 this.storageProvider.getToken().then(results => { this.httpOptions = { headers: new HttpHeaders({ 'Content-Type':

成功登录后,我在每个请求上都会收到未经授权的响应

这是我的一些代码(如果您需要查看其他内容,请告诉我):

爱奥尼亚的数据提供者

this.storageProvider.getToken().then(results => {
                      this.httpOptions = {
                      headers: new HttpHeaders({
                          'Content-Type': 'application/json',
                          'Authorization': 'Bearer ' + results,
                          'Accept': 'application/json',
                        })
                      };
                  });

public getTodayReservations() {
  //all reservations (not todays only)
    let _url = this.url + '/guides/reservations/all';
    return this.http.get(_url, this.httpOptions);
}
这是my laravel api路由的配置:

Route::prefix('v1')
->group(function () {

    Route::post('login', 'Api\UsersController@login');

    Route::middleware('auth:api')
        ->prefix('guides')
        ->group(function () {

            Route::get('/show', 'Api\UsersController@show');

            Route::get('/reservations/today', 'Api\ReservationsController@today');
            Route::get('/reservations/all', 'Api\ReservationsController@allRes');

        });
});
请求标头:

Accept: application/json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI.....
Content-Type: application/json
Origin: http://localhost:8100
Referer: http://localhost:8100/

您可以
this.storageProvider.getToken()
返回承诺,而不是令牌

试着这样做:

export class HttpService {

  private httpOptions;

  constructor(){
   this.storageProvider.getToken().then(results => {
        this.httpOptions = {
        headers: new HttpHeaders({
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + results,
            'Accept': 'application/json',
          })
        };
    });   
  }

检查
this.storageProvider.getToken()
的响应,通常它具有
access\u token
密钥,您应该使用该密钥对应用程序进行身份验证,以使您的代码,而不是完整的承诺,使您的代码如下所示:

this.storageProvider.getToken().then(tokenObject => {
    private httpOptions = {
        headers: new HttpHeaders(
            {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + tokenObject.access_token,
                'Accept': 'application/json',
            }
        )
    }; 
});

当然,在您的情况下,它可能是另一个键,这就是为什么您应该首先查看
this.storageProvider.getToken()
,并且在发送
Authorization
头时只发送实际的访问令牌。

我是否应该将其添加到构造函数中?在您定义了
httpOptions
的地方,只要用这个替换你的代码就行了不能这样做因为它是一个类httpOptions在我的例子中是一个属性初始化属性(实际上是一个属性)为NULL,然后在构造函数中放入代码来填充该属性,当然你会使用
this.httpOptions=
它工作,但仍然是相同的错误“unauthorized”检查我编辑的新代码即使现在我的请求头是:Accept:application/json Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI,帖子仍然有相同的消息错误。。。。。。内容类型:application/json来源:Referer: