错误 401:当 ESP32 使用 HttpRequest 更新 Cloud Firestore 上的数据时

问题描述 投票:0回答:0

现在我可以从 Cloud Firestore 获取数据以在 Arduino IDE 中显示,但是当我想更新 Cloud Firestore 上的数据时却无法做到。显示错误401。 apiKey:正确 项目ID:正确 WiFi连接:已连接 API 限制:无 我被这个问题困扰了4个小时

void updatePointsInFirestore(const String& documentID, int newPoints) {
  // Construct the Firestore URL for the specific document
  String firestoreURL = "https://firestore.googleapis.com/v1/projects/" + String(projectId) +
                        "/databases/(default)/documents/users/" + documentID;

  // Prepare the JSON payload with the new Points value
  String payload = "{\"fields\":{\"Points\":{\"integerValue\":" + String(newPoints) + "}}}";

  // Make the HTTP PATCH request to update the Points field
  HTTPClient http;
  http.begin(firestoreURL);

  // Add the Authorization header with the API Key
  http.addHeader("Authorization", "Bearer " + String(apiKey));
  http.addHeader("Content-Type", "application/json");

  int httpCode = http.sendRequest("PATCH", payload);

  if (httpCode == HTTP_CODE_OK) {
    Serial.println("Points updated successfully in Firestore.");
  } else {
    Serial.print("Error updating Points in Firestore. HTTP Response code: ");
    Serial.println(httpCode);
  }

  http.end(); // Close the connection
}

我尝试创建一个新项目来获取新的ProjectID和apiKey,但仍然错误。 我认为问题出在 ESP32 上

firebase http google-cloud-firestore httprequest esp32
© www.soinside.com 2019 - 2024. All rights reserved.