【发布时间】:2019-12-07 16:30:57
【问题描述】:
我正在尝试使用带有 Python 的 API 创建一个 Google Docs 文件。
我已按照他们的 API 指南和参考页面上的每条说明进行操作。
包括创建他们的快速启动脚本
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
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
def main():
SCOPES = ['https://www.googleapis.com/auth/drive.file']
"""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)
# Call the Drive v3 API
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print(u'{0} ({1})'.format(item['name'], item['id']))
title = 'My Document'
body = {
'title': title
}
doc = service.files() \
.create(body=body).execute()
print('Created document with title: {0}'.format(
doc.get('title')))
if __name__ == '__main__':
main()
我希望创建一个 Google Docs 文件,但脚本返回:已创建文档,标题为:无。
它没有返回任何错误,但显然它没有创建文件。
我很沮丧,因为我花了 9 个小时试图让 Google Drive 自己的脚本运行。该代码是 Google Drive and Docs API 文档的直接复制粘贴,除了我将范围从“SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']”更改为“SCOPES = ['https://www.googleapis.com/auth/drive.file']”,因为使用前者崩溃了,他们的 API 文档建议在尝试创建文件时使用后者作为范围。
编辑:
当前脚本:
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
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/documents']
def main():
"""Shows basic usage of the Docs API.
Prints the title of a sample document.
"""
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('docs', 'v1', credentials=creds)
title = 'My Document'
body = {
'title': title
}
doc = service.documents() \
.create(body=body).execute()
print('Created document with title: {0}'.format(
doc.get('title')))
return
if __name__ == '__main__':
main()
我收到以下错误:
追溯
(most recent call last):
File "create-teamwork-sops.py", line 137, in <module>
main()
File "create-teamwork-sops.py", line 131, in main
.create(body=body).execute()
File "C:\Python27\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Python27\lib\site-packages\googleapiclient\http.py", line 855, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://docs.googleapis.com/v1/documents?alt=json returned "Request had insufficient authentication scopes.">
注意:每次更改 SCOPES 的值时,都需要删除文件 token.pickle,脚本运行时会要求你登录 Google Drive 并创建一个新的 token.pickle 文件,该文件将允许考虑新的范围。
工作脚本:
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
SCOPES = ['https://www.googleapis.com/auth/drive']
def main():
"""Shows basic usage of the Docs API.
Prints the title of a sample document.
"""
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('docs', 'v1', credentials=creds)
service = build('drive', 'v3', credentials=creds)
# title = 'My Document'
# body = {
# 'title': title
# }
# doc = service.documents() \
# .create(body=body).execute()
# print('Created document with title: {0}'.format(
# doc.get('title')))
# get folder ID
page_token = None
while True:
response = service.files().list(q="mimeType = 'application/vnd.google-apps.folder'",
spaces='drive',
fields='nextPageToken, files(id, name)',
pageToken=page_token).execute()
for file in response.get('files', []):
# Process change
print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
if file.get('name')=="SOPs":
folder_id=file.get('id')
break
page_token = response.get('nextPageToken', None)
if page_token is None:
break
# create Google Docs file in folder
file_metadata = {
'name': 'my doc 2',
'parents': [folder_id]
}
# media = MediaFileUpload('files/photo.jpg',
# mimetype='image/jpeg',
# resumable=True)
file = service.files().create(body=file_metadata,
# media_body=media,
fields='id').execute()
print('File ID: %s' % file.get('id'))
return
if __name__ == '__main__':
main()
【问题讨论】:
-
我能问一下你的目标吗?您想使用 Drive API 还是 Docs API 创建新的 Google 文档?
-
文档显示使用 Google Docs API 创建文件。
-
感谢您的回复。我无法理解你的回复。我为我糟糕的英语水平道歉。
-
不用担心。我使用 Google Docs API 的目的是在我的 Google Drive 文件夹中创建 Google Docs 文件。
-
感谢您的回复。我能理解你的目标。所以我提出了一个修改后的脚本作为答案。你能确认一下吗?如果这不是您想要的结果,我深表歉意。
标签: google-drive-api