我的 Python 代码与 google 文档连接时出现问题 api

problems with my Python code connecting with google docs api

我正在编写一个 Python 应用程序以通过 google 文档 API 自动处理发票,但我的代码有错误,我不知道如何解决它

    # [START docs_quickstart]
from __future__ import print_function

import os.path
import gspread
from oauth2client.service_account import ServiceAccountCredentials

from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/documents.readonly','https://www.googleapis.com/auth/documents','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.readonly']



def main():
    """Shows basic usage of the Docs API.
    Prints the title of a sample document.
    """
    creds = None
    # The file token.json 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.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # 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.json', 'w') as token:
            token.write(creds.to_json())

    try:
        service = build('docs', 'v1', credentials=creds)

    

        title = 'My Document'
        body = {
        'title': title
                }
        document = service.documents().create(body=body).execute()
        print('Created document with title: {0}'.format(
        document.get('title')))
    except HttpError as err:
        print(err)
    

    
    


if __name__ == '__main__':
    main()
# [END docs_quickstart]

这是我遇到的错误代码

PS D:\Universidad\Proyectos de Programacion Propia\python\googledocsinvoice.restapi> python quickstart.py
<HttpError 403 when requesting https://docs.googleapis.com/v1/documents?alt=json returned "Request had insufficient authentication scopes.". Details: "[{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'ACCESS_TOKEN_SCOPE_INSUFFICIENT', 'domain': 'googleapis.com', 'metadata': {'service': 'docs.googleapis.com', 'method': 'google.apps.docs.v1.DocumentsService.CreateDocument'}}]">     

如果您知道使用 Google 文档 API 自动创建文档的一些指南或完整教程,例如发票和替换封装值,例如 {{productID}} 我将非常感激 我看到了 google 频道的自动文档创建,但它不是很有用 https://youtu.be/-dX-fWb3ogE

此致

  1. 确保您还在开发者控制台中启用了文档 API。

  2. 删除凭证文件 ~/.credentials.jsontoken.json(如果您 运行

    之前的代码
  3. 更改用于阅读文档的范围变量

var SCOPES = ['https://www.googleapis.com/auth/documents.readonly'];

var SCOPES = ['https://www.googleapis.com/auth/documents'];

查看有关 https://developers.google.com/docs/api/reference/rest/v1/documents/create#authorization-scopes and https://developers.google.com/docs/api/how-tos/authorizing

的更多信息
  1. 代码执行后,API将再次进行身份验证,然后问题应该得到解决。