Reputation: 3262
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?
Upvotes: 1
Views: 4099
Reputation: 628
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.
Upvotes: 0
Reputation: 116888
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.
change the scope to one of the ones required and request access of the user again.
Upvotes: 1