1

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?

2 Answers 2

1

Documents.create requires one of the following scopes

enter image description here

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@DalmTo Thank you, this mistake is gone, but there's a new problem <HttpError 400 when requesting https://docs.googleapis.com/v1/documents?alt=json returned "Invalid JSON payload received. Unknown name "": Root element must be a message.". Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'description': 'Invalid JSON payload received. Unknown name "": Root element must be a message.'}]}]"> Is it possible to open the transferred file without creating a new one?
Open a new question with that i will try and find time in the morning to take a look for you. You should probably be using the libray to build the body object but i am very novice Python dev i will have to look around for an example
@DalmTo thanks for your help, I found the most suitable solution for me. First, I save the file on the google drive, get its id and redirect it to google docs with the link containing the received id like return (f"https://docs.google.com/document/d/{file.get('id')}/edit")
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.