如何使用带有 firebase firestore Rest API 的自定义令牌文档 ref 发出 post 请求

How to make a post request with a custom token document ref with firebase firestore Rest API

下面是我的代码,我正在尝试使用 Firebase Firestore Rest API 发送 post 请求。它有效,但它会生成一个随机的 ref id 令牌,link将其发送到我发送的 post 数据(字段),这使得它很难关联。

我希望能够 link 我的 post 数据带有自定义 ID 令牌。

final String customIdToken9 = 'customIdToken9'
String url = "https://firestore.googleapis.com/v1/projects/myAppId/databases/(default)/documents/customers/$customIdToken9" 
const _body = {
"fields": {
    "androidNotificationToken": {
        "nullValue": null
    },
    "fullname": {
        "stringValue": "Custom"
    },
    "uid": {
        "stringValue": "$customIdToken9"
    },
    "admin": {
        "stringValue": ""
    },
    "email": {
        "stringValue": "customer1@gmail.com"
    },
    "photo": {
        "stringValue": ""
    },
    "coverPhoto": {
        "stringValue": ""
    },
    "bio": {
        "stringValue": ""
    },
    "role": {
        "stringValue": "user"
    },
    "mobile": {
        "stringValue": "092222"
    }
}
 }
   http.post(url, body = _body);

当我运行上面这段代码时returns

{
"error": {
    "code": 400,
    "message": "Document parent name \"projects/MyAppId/databases/(default)/documents/customers\" lacks \"/\" at index 59.",
    "status": "INVALID_ARGUMENT"
}

}

如果我删除 customTokenId9,它可以工作,但它会生成一个我不想要的随机令牌 ID

String url = "https://firestore.googleapis.com/v1/projects/myAppId/databases/(default)/documents/customers/" 

http.post(url, body = _body);

为什么你的变量是 final String customIdToken 但你正在使用 $customIdToken9 它应该是 $customIdToken.

如果您想指定自己的文档名称,您需要在 URL 请求中添加一个名为 documentId 的查询参数。它在您的代码中应该是这样的:

final String customIdToken9 = 'customIdToken9'
String url = "https://firestore.googleapis.com/v1/projects/myAppId/databases/(default)/documents/customers?documentId=$customIdToken9" 
const _body = {
"fields": {
    "androidNotificationToken": {
        "nullValue": null
    },
    "fullname": {
        "stringValue": "Custom"
    },
    "uid": {
        "stringValue": "$customIdToken9"
    },
    ...
  }
}

The client-assigned document ID to use for creating a document. It is optional. If not specified, a random ID will automatically be assigned by the service.

完整描述在此 link 中陈述,您将在“查询参数”部分中看到它。您还可以尝试测试 API 并展开它以查看它的 curl 等价物。

此外,您可以查看此 documentation 以了解查询参数的工作原理。它适用于 Firebase 提供的任何 REST API。