如何通过REST API对到Firebase Firestore的GET请求进行身份验证

时间:2020-02-25 19:05:41

标签: firebase google-cloud-firestore firebase-authentication

我想通过REST API对Firestore执行经过身份验证的GET请求。将Firestore规则设置为以下条件时,我可以成功执行GET请求:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

我为此使用的GET请求的格式如下:

https://firestore.googleapis.com/v1/projects/YOUR_PROJECT_ID/databases/(default)/documents/cities/LA

但是,如果我添加身份验证并将规则更改为:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

我需要能够验证我的GET请求。我可以采用以下格式发出POST请求:

https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=[API_KEY]&email=[EMAIL]&password=[PASSWORD]

这将返回一个idToken,我想用它来认证我的GET请求。

我该怎么做?我尝试使用新的idToken将KEY添加到我的GET请求中,但是我没有运气。

1 个答案:

答案 0 :(得分:0)

获得ID令牌后,可以使用带有承载令牌的Authorization标头将其传递给REST API:

Authorization: Bearer ${idToken}

有关更多信息,请参见the docs