Firestore REST API 使用 http-client 发出经过身份验证的请求
Firestore REST API make authenticated request with http-client
我想通过 REST API 从 firestore 获取数据。我正在使用 HTTP 客户端 (Webstorm) 并执行以下操作。
首先我使用 Google 进行身份验证,它工作正常并且 return 一个令牌:
POST https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=<firebase-api-key>
Accept: application/json
Content-Type: application/json
{
"email": "some@email.com",
"password": "notthispassword"
}
但是,尝试像这样从 firestore(不是实时数据库)获取数据
GET https://firestore.googleapis.com/v1/projects/<projectId>/databases/(default)/documents/<collection>
Accept: application/json
Authorization: Bearer <token from auth response>
它一直告诉我:
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
这些是我的 Firestore 安全规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
如果有人能告诉我哪里出了问题,我会很高兴。
解决方案的第一部分是仔细阅读回复。它包含以下 link https://developers.google.com/identity/sign-in/web/devconsole-project.
然后我不得不明白,如果您使用的是 google-identitiy-toolkit,那么您有点离开了 firebase-realm 并且 必须附加生成的 api-key在 GC-console(不是 firebase-key!)(https://console.cloud.google.com/apis/credentials)到 URL 用于获取这样的数据:
GET https://firestore.googleapis.com/v1/projects/<projectId>/databases/(default)/documents/<collection>?key=<google-cloud-api-key>
Accept: application/json
Authorization: Bearer <token from auth response>
我想通过 REST API 从 firestore 获取数据。我正在使用 HTTP 客户端 (Webstorm) 并执行以下操作。
首先我使用 Google 进行身份验证,它工作正常并且 return 一个令牌:
POST https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=<firebase-api-key>
Accept: application/json
Content-Type: application/json
{
"email": "some@email.com",
"password": "notthispassword"
}
但是,尝试像这样从 firestore(不是实时数据库)获取数据
GET https://firestore.googleapis.com/v1/projects/<projectId>/databases/(default)/documents/<collection>
Accept: application/json
Authorization: Bearer <token from auth response>
它一直告诉我:
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
这些是我的 Firestore 安全规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
如果有人能告诉我哪里出了问题,我会很高兴。
解决方案的第一部分是仔细阅读回复。它包含以下 link https://developers.google.com/identity/sign-in/web/devconsole-project.
然后我不得不明白,如果您使用的是 google-identitiy-toolkit,那么您有点离开了 firebase-realm 并且 必须附加生成的 api-key在 GC-console(不是 firebase-key!)(https://console.cloud.google.com/apis/credentials)到 URL 用于获取这样的数据:
GET https://firestore.googleapis.com/v1/projects/<projectId>/databases/(default)/documents/<collection>?key=<google-cloud-api-key>
Accept: application/json
Authorization: Bearer <token from auth response>