首页 > 解决方案 > 如何使用 Google Apps 脚本将数据写入 Firebase Cloud Firestore

问题描述

我正在尝试使用 Google Apps 脚本将测试数据写入我的 Firebase Cloud Firestore。运行我的脚本后writeDataToFirestore(),我希望看到我的 GAS 和我的 firestore 中的测试数据成功执行。但是,相反,我收到以下错误。

执行失败:无效参数:密钥(第 57 行,文件“Authenticate”,项目“FirestoreApp”)

我按照此处所有文档的说明从云平台获取我的client_email,private_key和。project_id

代码.gs
function writeDataToFirestore() {
  // library: 1VUSl4b1r1eoNcRWotZM3e87ygkxvXltOgyDZhixqncz9lQ3MjfT1iKFw

  var email = my.client_email;
  var key = my.private_key;
  var projectId = my.project_id;

  var firestore = FirestoreApp.getFirestore(email, key, projectId);

  const data = {
    "name": "test!"
  };

  firestore.createDocument("FirstCollection", data);
}

这是整个执行记录。

执行记录
[19-10-25 15:45:32:675 PDT] 开始执行
[19-10-25 15:45:32:719 PDT] Utilities.base64EncodeWebSafe([{"alg":"RS256","typ ":"JWT"}]) [0 秒]
[19-10-25 15:45:32:719 PDT] Utilities.base64EncodeWebSafe([{"scope":" https://www.googleapis.com/auth/数据存储","aud":" https://oauth2.googleapis.com/token ","exp":1572047132.691,"iat":1572043532.691}]) [0 秒]
[19-10-25 15:45:32 :723 PDT] Utilities.computeRsaSha256Signature([eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvZGF0YXN0b3JlIiwiYXVkIjoiaHR0cHM6Ly9vYXV0aDIuZ29vZ2xlYXBpcy5jb20vdG9rZW4iLCJleHAiOjE1NzIwNDcxMzIuNjkxLCJpYX...) [0. 003秒]
[19-10-25 15:45:32:732 PDT] 执行失败:无效参数:密钥(第 57 行,文件“Authenticate”,项目“FirestoreApp”)[0.033 秒总运行时间]

标签: firebasegoogle-apps-scriptgoogle-cloud-platformgoogle-cloud-firestore

解决方案


推荐阅读