简体   繁体   English

如何使用 google api 和 python 在 google docs 中打开文件?

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

I try to open file sent by an application written in python/django.我尝试打开用 python/django 编写的应用程序发送的文件。 Usin documentation link for creating blank document I trying to transfer an existing file to open in google docs.使用用于创建空白文档的文档链接我尝试传输现有文件以在 google 文档中打开。

views.py视图.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')))

I put the value of import_file as the value of the variable body and I get an error我把import_file的值作为变量的值,我得到一个错误

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

How do I properly transfer an external file to google docs for opening?如何将外部文件正确传输到 google 文档以打开?

Documents.create requires one of the following scopes Documents.create需要以下范围之一

在此处输入图片说明

You are using https://www.googleapis.com/auth/documents.readonly which gives you the error您正在使用https://www.googleapis.com/auth/documents.readonly这会给您带来错误

Request had insufficient authentication scopes.请求的身份验证范围不足。

solution解决方案

change the scope to one of the ones required and request access of the user again.将范围更改为所需范围之一并再次请求用户访问。

It depends why you want this.这取决于你为什么想要这个。 Above is the right way, using google's api, but You can also use the following hack:以上是正确的方法,使用google的api,但您也可以使用以下hack:
url = https://docs.google.com/document/u/0/ webbrowser.open(url) url = https://docs.google.com/document/u/0/ webbrowser.open(url)

then use pyautogui to maximize or tile the window and click new.然后使用 pyautogui 最​​大化或平铺窗口并单击新建。 This only works for one sized display.这仅适用于一种尺寸的显示器。 If you move to another system, you'll need to change where pyautogui clicks.如果您移动到另一个系统,则需要更改 pyautogui 单击的位置。 Also if Google changes the page layout, it will probably break.此外,如果 Google 更改页面布局,它可能会中断。

I do this with my voice assistant when I want to dictate a note.当我想听写一个音符时,我会用我的语音助手来做这件事。 It's just for me and not for distribution;它只适合我,不适合分发; so why not?那为什么不呢? I don't want my api key in my code out on github.我不希望我的代码中的 api 密钥在 github 上出现。 This is a pain for a lot of people who share their code but need api keys.对于许多共享代码但需要 api 密钥的人来说,这很痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM