Firestore REST API 使用 http-client 發出經過身份驗證的請求

菲爾

我想通過 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Firestore的自定义Rest Api Java身份验证

通过Rest API使用Javascript发送Http POST请求

Firestore REST API开始查询

Github Pages网站使用HTTPS,但是Rest API使用HTTP

Firestore使用Rest API更新文档字段

带有ID令牌的REST API Firestore身份验证

使用Firestore REST API返回HTTP 400创建新的集合和文档

使用Firestore创建类似REST的安全API

使用Firestore REST API进行管理访问

使用Firestore REST API在StructuredQuery中设置“ orderBy”

使用Firestore REST API更新文档字段

可以一起使用Zend_Http_Client()和Zend_Rest_Client()

使用python http.client访问korbit api

如何使用node-rest-client查看原始HTTP请求和原始HTTP响应

如何使用 http.client for API 应用带有身份验证的代理

如何使用 firestore 作为 REST API 的后端

强制 Zappa 使用 API Gateway HTTP API 而不是 REST

API:您是否需要隨每個請求發送身份驗證標頭令牌?

使用 Qlik Data Catalyst REST API 進行身份驗證

請求的身份驗證範圍不足 - gmail api 離線無法使用刷新令牌

Django rest 框架:創建用戶請求身份驗證

作為經過身份驗證的用戶,我可以使用 ArcGIS REST-API“applyEdits”來更新要素圖層嗎?

如何驗證是否已使用 http_mock_adapter 發出請求?

使用 PKCE 的 Azure DevOps REST API 身份驗證

使用休息請求通過瀏覽器身份驗證 - Gmail API

使用 Python 向 Google API 發出請求

可以使用 JWT 令牌從 Google Fitness REST API 檢索數據進行身份驗證嗎?

Firestore 文档未使用 REST API 递增

无法使用 http Dart 包对 Azure DevOps Rest API 进行身份验证