Выполните шаги 1 и 2, как описано в следующей ссылке:
https://developers.google.com/docs/api/quickstart/python
А затем попробуйте следующий код:
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/documents']
creds = None
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('/content/credentials.json', SCOPES)
#creds = flow.run_local_server(port=0)
creds = flow.run_console()
# 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 Test Document'
body = {
'title': title
}
doc = service.documents().create(body=body).execute()
docId = doc.get('documentId')
print( 'Id of new Document is : ' + docId )
ПРИМЕЧАНИЕ: Поместите файл credentials.json
в соответствующее место и соответственно укажите путь в коде.