简体   繁体   中英

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. Usin documentation link for creating blank document I trying to transfer an existing file to open in google docs.

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')))

I put the value of import_file as the value of the variable body and I get an error

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?

Documents.create requires one of the following scopes

在此处输入图片说明

You are using https://www.googleapis.com/auth/documents.readonly which gives you the error

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:
url = https://docs.google.com/document/u/0/ webbrowser.open(url)

then use pyautogui to maximize or tile the window and click new. This only works for one sized display. If you move to another system, you'll need to change where pyautogui clicks. Also if Google changes the page layout, it will probably break.

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. This is a pain for a lot of people who share their code but need api keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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