How do I solve “AttributeError: 'Resource' object has no attribute 'documents'” error?
How do I solve “AttributeError: 'Resource' object has no attribute 'documents'” error?
我最近 3 天都在努力。我不知道..有类似的问题,但我没有得到答案。
这是错误
File ID: 1U4XUrAhMk1WFAKE_IDqmQcteYqmIWPMFEd Traceback (most recent
文件已创建我想使用此 ID 并在云端硬盘中编辑文档
call last): File "main.py", line 61, in
main() File "main.py", line 53, in main
service.documents() AttributeError: 'Resource' object has no attribute 'documents'
我的目标
在 GOOGLE 驱动器
中创建文档
在其中插入Table
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import sys
from gdoctableapppy import gdoctableapp
# If modifying these scopes, delete the file token.pickle.
SCOPES = ["https://www.googleapis.com/auth/drive"]
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
service = build("drive", "v3", credentials=creds)
serviceDoc = build("docs", "v1", credentials=creds)
# Call the Drive v3 API
# Create Google Docs file in folder
file_metadata = {
"name": sys.argv[1],
"parents": ["Folder ID"],
}
file = service.files().create(body=file_metadata, fields="id").execute()
print("File ID: %s" % file.get("id"))
DOCUMENT_ID = file.get("id")
requests = [{"insertTable": {"rows": 2, "columns": 2, "location": {"index": 1}}}]
result = (
service.documents()
.batchUpdate(documentId=DOCUMENT_ID, body={"requests": requests})
.execute()
)
return
if __name__ == "__main__":
main()
您遇到此类错误的原因是因为您的 service
变量用于驱动器 API,它没有 documents()
方法。
改用serviceDoc
:
serviceDoc.documents()
.batchUpdate(documentId=DOCUMENT_ID, body={"requests": requests})
.execute()
另外:
我注意到当您创建 Docs 文件时 mimeType
不是您的 file_metadata
的一部分。如果您创建的文件没有特定的 mimeType
,您新创建的文件将为 application/octet-stream
。参见 Create Files
如果您想使用云端硬盘 API 创建 Google 文档,请在 file_metadata
中添加 "mimeType"='application/vnd.google-apps.document'
样本:
file_metadata = {
"name": sys.argv[1],
"mimeType"='application/vnd.google-apps.document',
"parents": ["Folder ID"]
}
参考:
我最近 3 天都在努力。我不知道..有类似的问题,但我没有得到答案。
这是错误
File ID: 1U4XUrAhMk1WFAKE_IDqmQcteYqmIWPMFEd Traceback (most recent
文件已创建我想使用此 ID 并在云端硬盘中编辑文档
call last): File "main.py", line 61, in
main() File "main.py", line 53, in main
service.documents() AttributeError: 'Resource' object has no attribute 'documents'
我的目标
在 GOOGLE 驱动器
中创建文档在其中插入Table
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import sys
from gdoctableapppy import gdoctableapp
# If modifying these scopes, delete the file token.pickle.
SCOPES = ["https://www.googleapis.com/auth/drive"]
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
service = build("drive", "v3", credentials=creds)
serviceDoc = build("docs", "v1", credentials=creds)
# Call the Drive v3 API
# Create Google Docs file in folder
file_metadata = {
"name": sys.argv[1],
"parents": ["Folder ID"],
}
file = service.files().create(body=file_metadata, fields="id").execute()
print("File ID: %s" % file.get("id"))
DOCUMENT_ID = file.get("id")
requests = [{"insertTable": {"rows": 2, "columns": 2, "location": {"index": 1}}}]
result = (
service.documents()
.batchUpdate(documentId=DOCUMENT_ID, body={"requests": requests})
.execute()
)
return
if __name__ == "__main__":
main()
您遇到此类错误的原因是因为您的 service
变量用于驱动器 API,它没有 documents()
方法。
改用serviceDoc
:
serviceDoc.documents()
.batchUpdate(documentId=DOCUMENT_ID, body={"requests": requests})
.execute()
另外:
我注意到当您创建 Docs 文件时 mimeType
不是您的 file_metadata
的一部分。如果您创建的文件没有特定的 mimeType
,您新创建的文件将为 application/octet-stream
。参见 Create Files
如果您想使用云端硬盘 API 创建 Google 文档,请在 file_metadata
"mimeType"='application/vnd.google-apps.document'
样本:
file_metadata = {
"name": sys.argv[1],
"mimeType"='application/vnd.google-apps.document',
"parents": ["Folder ID"]
}
参考: