我想通過 REST API 從 Firestore 獲取數據。我正在使用 HTTP 客戶端 (Webstorm) 並執行以下操作。
首先,我使用 Google 進行身份驗證,它工作正常並返回一個令牌:
POST https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=<firebase-api-key>
Accept: application/json
Content-Type: application/json
{
"email": "[email protected]",
"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;
}
}
}
如果有人能告訴我我哪裡出錯了,我會很高興。
解決方案的第一部分是仔細閱讀回复。它包含以下鏈接https://developers.google.com/identity/sign-in/web/devconsole-project。
然後我必須明白,如果您使用的是 google-identitiy-toolkit,那麼您就離開了 firebase-realm 並且必須附加在 GC-console 中生成的 api-key(不是 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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句