最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

节点Google Docs REST API批量更新无效的JSON有效负载缺少documentId

运维笔记admin8浏览0评论

节点Google Docs REST API批量更新无效的JSON有效负载/缺少documentId

节点Google Docs REST API批量更新无效的JSON有效负载/缺少documentId

尝试执行批量更新时,我收到无效的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\              mon\build\src\apirequest.js:114:19
at Generator.next (<anonymous>)

我最好的猜测是,为了成功编码JSON对象以使请求成功,需要进行某种谷歌巫术魔术。

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

这个修改怎么样?

Modification points:

  • insertTextRequest的属性修改为insertText
  • 关于updateObject,请使用resource的财产来提出请求正文。

Modified script:

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);
    }
});

Note:

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

References:

  • documents.batchUpdate
  • InsertTextRequest

如果我误解了你的问题,我道歉。

发布评论

评论列表(0)

  1. 暂无评论