python - Google api 批量更新给出 HTTP 400 错误。Python
问题描述
用于文档的 google api 给了我 HTTP 400 错误。
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://docs.googleapis.com/v1/documents/1Z2rAi7CJINAwxzizOsIw0_UB1iguHaduIDBV5cOZ4iQ:batchUpdate?alt=json returned "Invalid requests[0].insertText: The insertion index must be inside the bounds of an existing paragraph. You can still create new paragraphs by inserting newlines.">
我到处找找它。它是插入一个空白文档。它说要添加一个新段落,但我不知道该怎么做。
title = 'My Document'
text1 = "hi this is the first one"
text2 = "hi this is the first one"
text3 = "hi this is the first one"
body = {
'title': title
}
requests = [
{
'insertText': {
'location': {
'index': 0,
},
'text': text1
}
}
]
doc = service.documents().create(body=body).execute()
print('Created document with title: {0}'.format(
doc.get('title')))
docid = doc.get('documentId')
print(docid)
result = service.documents().batchUpdate(documentId=docid, body={'requests': requests}).execute()
完整的错误消息是这样的:
"E:/Python/News Uploader to Google Driver/venv/Scripts/quickstart.py" Created document with title: My Document 1Z2rAi7CJINAwxzizOsIw0_UB1iguHaduIDBV5cOZ4iQ Traceback (most recent call last): File "E:/Python/News Uploader to Google Driver/venv/Scripts/quickstart.py", line 76, in <module> main() File "E:/Python/News Uploader to Google Driver/venv/Scripts/quickstart.py", line 70, in main result = service.documents().batchUpdate(documentId=docid, body={'requests': requests}).execute() File "E:\Python\News Uploader to Google Driver\venv\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper return wrapped(*args, **kwargs) File "E:\Python\News Uploader to Google Driver\venv\lib\site-packages\googleapiclient\http.py", line 856, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 400 when requesting https://docs.googleapis.com/v1/documents/1Z2rAi7CJINAwxzizOsIw0_UB1iguHaduIDBV5cOZ4iQ:batchUpdate?alt=json returned "Invalid requests[0].insertText: The insertion index must be inside the bounds of an existing paragraph. You can still create new paragraphs by inserting newlines."> Process finished with exit code 1
解决方案
在您的请求中,您应该将位置索引指定为 1,因为 Google API 是从 1 开始索引的
requests = [
{
'insertText': {
'location': {
'index': 1,
},
'text': text1
}
}
]
推荐阅读
- fonts - SwiftUI 是否支持按首选字体大小的替代布局?
- node.js - restify 服务器中的 CORS 给我带来了麻烦
- vb6 - ms access 数据库自动启动再次存储第一行的数据
- swift - 斯威夫特:不能在循环中解开一个可选的
- .net - System.IO.FileNotFoundException:无法加载文件或程序集 Microsoft.Extensions.Caching.Abstractions
- python - 读取/写入文件 - JSON - 正文
- javascript - 我收到以下错误“无法在 'Node' 上执行 'insertBefore'”
- html - 如何在 Outlook 中获取图像以填充表格单元格的宽度?
- ansible - Ansible Tower - 模块 lineinfile - 问题:缺少行
- c - 在C中返回联合的有效方法?