Node Google Docs REST API 批量更新无效JSON payload/missing documentId
Node Google Docs REST API Batch update invalid JSON payload/missing documentId
当我尝试执行批量更新时收到无效 JSON 负载错误;并尝试将 JSON 字符串化会给出 'missing documentId' 错误。问题不在于 OAuth、令牌或文档范围,因为它们都是正确且有效的(范围已从示例的只读更改为完整文档范围)。
由于在节点中没有 Google 的新批量更新 API 的示例,因此我在批量更新方面遇到了重大问题。在使用 batchUpdate 构造函数进行一些故障排除后,我已经将我的问题缩小到(可能)更大的 API url 构造函数问题,或者我的语法错误(或两者都有)。或者我缺少步骤为 API 调用创建适当的对象(这些任务没有文档)
根据 google 节点快速入门指南(大部分)成功获取文档后的回调内部
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)
}
GoogleAPI响应
'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 巫毒魔法才能正确编码 JSON 对象以使请求成功。
- 将单个请求的数组更改为对象对上述没有影响。
- 对请求对象参数 names/string 变量使用 single/double 引号无效。
- 文档 ID 是一个字符串,有效,只是代码示例中没有显示。
- 向请求添加 documentId 字段没有效果。
这个修改怎么样?
修改点:
- 将
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);
}
});
注:
- 如果出现status
INVALID_ARGUMENT
和message"Invalid requests[0].insertText: Index ### must be less than the end index of the referenced segment, ##."的错误,例如,请尝试将index
修改为1
。
- 如果出现错误,请尝试使用最新版本的googleapis,因为最近添加了Docs API。
- 此修改后的脚本假定访问令牌包括
https://www.googleapis.com/auth/documents
的范围并且文档 API 已启用。
参考文献:
如果我误解了你的问题,我深表歉意。
当我尝试执行批量更新时收到无效 JSON 负载错误;并尝试将 JSON 字符串化会给出 'missing documentId' 错误。问题不在于 OAuth、令牌或文档范围,因为它们都是正确且有效的(范围已从示例的只读更改为完整文档范围)。
由于在节点中没有 Google 的新批量更新 API 的示例,因此我在批量更新方面遇到了重大问题。在使用 batchUpdate 构造函数进行一些故障排除后,我已经将我的问题缩小到(可能)更大的 API url 构造函数问题,或者我的语法错误(或两者都有)。或者我缺少步骤为 API 调用创建适当的对象(这些任务没有文档)
根据 google 节点快速入门指南(大部分)成功获取文档后的回调内部
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)
}
GoogleAPI响应
'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 巫毒魔法才能正确编码 JSON 对象以使请求成功。
- 将单个请求的数组更改为对象对上述没有影响。
- 对请求对象参数 names/string 变量使用 single/double 引号无效。
- 文档 ID 是一个字符串,有效,只是代码示例中没有显示。
- 向请求添加 documentId 字段没有效果。
这个修改怎么样?
修改点:
- 将
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);
}
});
注:
- 如果出现status
INVALID_ARGUMENT
和message"Invalid requests[0].insertText: Index ### must be less than the end index of the referenced segment, ##."的错误,例如,请尝试将index
修改为1
。 - 如果出现错误,请尝试使用最新版本的googleapis,因为最近添加了Docs API。
- 此修改后的脚本假定访问令牌包括
https://www.googleapis.com/auth/documents
的范围并且文档 API 已启用。
参考文献:
如果我误解了你的问题,我深表歉意。