Firebase:无法解析身份验证令牌

时间:2017-06-24 00:56:48

标签: angularjs authentication firebase

首次Stack Overflow海报在这里!

我正在关注Angular 4教程,并使用Firebase完成其身份验证部分(链接:)。我能够成功注册并登录,但在通过'getIdToken'将用户'令牌传递给我的GET请求时会收到错误,以便将某些操作限制为经过身份验证的用户。

将我的令牌传递给我的GET请求时,我收到以下错误:

* Response {_body: "{↵  "error" : "Could not parse auth token."↵}↵", status: 401, ok: false, statusText: "Unauthorized", headers: Headers…}

从控制台复制和粘贴令牌到代码

时,我也会遇到此问题

我已经发布了以下(可能)最相关的代码进行调试:

header.component.ts

  onFetchData() {
    this.dataStorageService.getRecipes();
  }

数据storage.service.ts

  getRecipes() {

    const token = this.auth.getTokenGrabber() 

    this.http.get('https://recipes-fe1ba.firebaseio.com/.json?auth=' + token)
      .map(
        (response: Response) => {
          console.log(response.json())
          const recipes: Recipe[] = response.json();
          for (let recipe of recipes) {
            if (!recipe['ingredients']) {
              recipe['ingredients'] = [];
            }
          }
          return recipes;
        }
      )
      .subscribe(
        (recipes: Recipe[]) => {
          this.recipeService.setRecipes(recipes);
        }
      );
  }

authentication.service.ts

 signinUser(email, password){

    firebase.auth().signInWithEmailAndPassword(email, password)
      .then(
        (response) => {
              firebase.auth().currentUser.getIdToken(
               ).then(
                  (token:string) => {
                    // console.log(token)  
                    this.token = token 
                    console.log('user was found')
                    // console.log(this.token)
                  }
               )
        }
      ).catch((erorr) => {
        console.log('user was not found')    
      }
    )  
  }

  getTokenGrabber(){
            firebase.auth().currentUser.getIdToken()
              .then(
                  (token:string) =>

                    this.token = token 
               )
               return this.token
        }
}

1 个答案:

答案 0 :(得分:0)

REST API documentation表示查询参数为access_token,而不是auth。试一试:

this.http.get('https://recipes-fe1ba.firebaseio.com/.json?access_token=' + token)
相关问题