Vue.js + Firebase:401错误,“未经授权”

时间:2019-03-15 19:51:00

标签: firebase vue.js

当我尝试在Vue项目中使用post方法时:

this.$http.post("https://vuejs-blog-b91df.firebaseio.com/posts.json", this.blog).then(function(data) {
   console.log(data);
   this.submitted = true;}

控制台向我抛出错误:

Failed to load resource: the server responded with a status of 401 (Unauthorized)

Uncaught (in promise) 
Response
body: {error: "Permission denied"}
bodyText: "{↵  "error" : "Permission denied"↵}↵"
headers: Headers {map: {…}}
ok: false
status: 401
statusText: "Unauthorized"
url: "https://project-name.firebaseio.com/posts.json"
data: (...)
__proto__: Object

我在哪里以及如何将Firebase项目apiKey和其他授权数据成功放置?

1 个答案:

答案 0 :(得分:0)

使用access_token进行身份验证

基于firbase认证documentation,我认为您必须将请求URL更改为如下所示:

<ul>
  <li v-for="(post, index) of paginatedPosts" class="post">
    ...
  </li>
</ul>

使用ID令牌进行身份验证

或者您可以像这样使用ID令牌认证:

const token = 'token'; // get token somewhere
this.$http
  .post(`https://vuejs-blog-b91df.firebaseio.com/posts.json?access_token=${token}`, this.blog)
  .then((data) => {
     console.log(data);
     this.submitted = true;
  });

Here,您可以找到如何在客户端上获取ID令牌