【问题标题】:Node Google Docs REST API Batch update invalid JSON payload/missing documentId节点 Google Docs REST API 批量更新无效 JSON 有效负载/缺少 documentId
【发布时间】:2019-07-06 17:30:38
【问题描述】:

尝试执行批量更新时收到 Invalid JSON payload 错误;并尝试对 JSON 进行字符串化会出现“缺少 documentId”错误。问题不在于 OAuth、令牌或文档范围,因为它们都是正确且正常运行的(范围从示例的只读更改为完整文档范围)。

由于节点中没有 Google 新的批量更新 API 示例,我在批量更新方面遇到了重大问题。在对 batchUpdate 构造函数进行一些故障排除后,我已将问题缩小到(可能)API url 构造函数的更大问题,或者我的语法错误(或两者兼而有之)。或者我缺少为 API 调用创建适当对象的步骤(这些任务没有文档)

根据谷歌节点快速入门指南成功获取文档后的回调内部(大部分)

    let offset = startIndex + 12
    let updateObject = {
      documentId:doc_id,
      requests:
        [{
          insertTextRequest : 
            {
              text : 'John Doe',
              location : {
                index : offset
              }
            }
        }]
      } 
    docs.documents.batchUpdate(updateObject,function(e,r){
      console.log(e)
      console.log(r)
    }

Google API 响应

'Invalid JSON payload received. Unknown name "requests[insertText][location][index]": Cannot bind query parameter. Field \'requests[insertText][location][index]\' could not be found in request message.\nInvalid JSON payload received. Unknown name "requests[insertText][text]": Cannot bind query parameter. Field \'requests[insertText][text]\' could not be found in request message.',
   domain: 'global',
   reason: 'badRequest' } ] }

尝试 JSON.stringify(updateObject) 后的响应 - 被截断

Error: Missing required parameters: documentId
at node_modules\googleapis-common\build\src\apirequest.js:114:19
at Generator.next (<anonymous>)

我的最佳猜测是 API 需要发生某种 google voodoo 魔法才能正确编码 JSON 对象以使请求成功。

  • 将单个请求的数组更改为对象对上述没有影响。
  • 对请求对象参数名称/字符串变量使用单引号/双引号无效。
  • 文档 ID 是一个字符串,有效,只是代码示例中没有显示。
  • 向请求中添加 documentId 字段无效。

【问题讨论】:

    标签: node.js json rest google-api google-docs-api


    【解决方案1】:

    这个修改怎么样?

    修改点:

    • 修改insertTextRequest的属性为insertText
    • 关于updateObject,请使用resource的属性放置请求体。

    修改脚本:

    let offset = startIndex + 12;
    let updateObject = {
        documentId: doc_id,
        resource: {
            requests: [{
                "insertText": {
                    "text": "John Doe",
                    "location": {
                        "index": offset,
                    },
                },
            }],
        },
    };
    docs.documents.batchUpdate(updateObject, function(e, r) {
        if (e) {
            console.log(e);
        } else {
            console.log(r.data);
        }
    });
    

    注意:

    • 如果状态错误INVALID_ARGUMENT 和消息“无效请求[0].insertText:索引### 必须小于引用段的结束索引,##。”例如,请尝试将index修改为1
    • 如果出现错误,请尝试使用最新版本的googleapis,因为最近添加了Docs API。
    • 此修改后的脚本假设访问令牌包含https://www.googleapis.com/auth/documents 的范围并启用了Docs API。

    参考资料:

    如果我误解了您的问题,我深表歉意。

    【讨论】:

    • 成功了!我在发布问题时尝试使用 insertText,但看到文档列为 insertTextRequest。我错过了请求的封闭“资源”对象。感谢您的及时回复!
    • @redcap3000 感谢您的回复。我很高兴你的问题得到了解决。现在我通过参考Spreadsheet的示例脚本来使用Docs API和node.js,因为官方文档还在增长。
    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 2017-11-26
    • 2019-08-04
    • 2018-07-31
    • 2023-04-03
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多