尝试执行批量更新时,我收到无效的JSON有效负载错误;并尝试stringify JSON给出'missing 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>)
我最好的猜测是,为了成功编码JSON对象以使请求成功,需要进行某种谷歌巫术魔术。
这个修改怎么样?
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
。https://www.googleapis.com/auth/documents
的范围,并且启用了Docs API。如果我误解了你的问题,我道歉。