我正在尝试使用Google API创建新的Google文档(用于自动审核请求)
我正在使用Google的“ Example”中的以下代码,但对我不起作用:https://github.com/gsuitedevs/python-samples/blob/master/docs/quickstart/quickstart.py
执行脚本时出现错误:
Traceback (most recent call last):
File "google_docs.py", line 46, in <module>
main()
File "google_docs.py", line 31, in main
'credentials.json', SCOPES)
File "/usr/local/lib/python3.7/site-packages/google_auth_oauthlib/flow.py", line 199, in from_client_secrets_file
return cls.from_client_config(client_config, scopes=scopes, **kwargs)
File "/usr/local/lib/python3.7/site-packages/google_auth_oauthlib/flow.py", line 159, in from_client_config
'Client secrets must be for a web or installed app.')
ValueError: Client secrets must be for a web or installed app.
注意:我已经创建了一个服务帐户,并且以下代码使用相同的凭据也可以正常工作:
import gspread
client = gspread.service_account(filename='credentials.json')
sheet = client.open('Test').sheet1
print(sheet.get_all_records())
所以,我想这意味着凭证文件是正确的,但是我想知道可以更改什么以能够创建Google文档,而不仅仅是电子表格...
经过几个小时的试验和沮丧,我发现了一个肮脏的解决方法:
import gspread
from googleapiclient.discovery import build
# The ID of a sample document.
DOCUMENT_ID = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE'
client = gspread.service_account(filename='credentials.json')
service = build('docs', 'v1', credentials=client.auth)
document = service.documents().get(documentId=DOCUMENT_ID).execute()
print('The title of the document is: {}'.format(document.get('title')))
所以,我将其放在此处,以防万一有人遇到今天遇到的同样问题:)