【发布时间】: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