Search code examples
pythongoogle-apigoogle-docs-apigoogle-api-python-client

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?


Solution

  • 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.