如何使用 google api 和 python 打开 google 文档中的文件?

How do I open a file in google docs using google api and python?

我尝试打开用 python/django 编写的应用程序发送的文件。使用文档 link 创建空白文档 我尝试传输现有文件以在 google 文档中打开。

views.py

SCOPES = ['https://www.googleapis.com/auth/documents.readonly']
def file_to_gd_docs(import_file=None):
    .... 
    #authorization part(success)
    ....
    service = build('docs', 'v1', credentials=creds)

    title = 'My Document'
    body = {
        'title': title
    }
    doc = service.documents().create(body=import_file).execute()
    print('Created document with title: {0}'.format(
        doc.get('title')))

我把import_file的值作为变量body的值 我得到一个错误

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://docs.googleapis.com/v1/documents?alt=json returned "Request had insufficient authentication scopes.">

如何正确地将外部文件传输到 google 文档以便打开?

Documents.create 需要以下范围之一

您正在使用 https://www.googleapis.com/auth/documents.readonly,这给了您错误

Request had insufficient authentication scopes.

解决方案

将范围更改为所需范围之一并再次请求用户访问。

这取决于你为什么想要这个。以上是正确的方法,使用 google 的 api,但您也可以使用以下 hack:
url = https://docs.google.com/document/u/0/ webbrowser.open(url)

然后使用 pyautogui 最​​大化或平铺 window 并单击新建。这仅适用于一种尺寸的显示器。如果您移动到另一个系统,则需要更改 pyautogui 单击的位置。此外,如果 Google 更改页面布局,它可能会中断。

当我想听写一个音符时,我会用我的语音助手这样做。仅供我使用,不用于分发;那为什么不呢?我不希望 github 上我的代码中的 api 键。对于许多共享代码但需要 api 密钥的人来说,这是一个痛苦。