0

I can't write data to google sheets via python. I did everything like in Google Sheets Api example but it still doesn't work. This is my project:

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools

SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file']

SAMPLE_SPREADSHEET_ID = 'ID'
SAMPLE_RANGE = 'Sheet2!A1:A10'

def main():
    store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
    creds = tools.run_flow(flow, store)

service = build('sheets', 'v4', http=creds.authorize(Http()))

values = {'values': [['one','two','three']]}
result = service.spreadsheets().values().append(
    spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE,
    valueInputOption='RAW',
    body=values).execute()
print('{0} cells updated.'.format(result.get('updatedCells')));

if __name__ == '__main__':
    main()

And it gives me this errors:

C:\Users\Victor\Anaconda3\python.exe "C:/Users/Victor/Desktop/NEW try/try1.py"
Traceback (most recent call last):
File "C:/Users/Victor/Desktop/NEW try/try1.py", line 31, in <module>
    main()
    File "C:/Users/Victor/Desktop/NEW try/try1.py", line 27, in main
    body=values).execute()
    File "C:\Users\Victor\Anaconda3\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
File "C:\Users\Victor\Anaconda3\lib\site-packages\googleapiclient\http.py", line 842, in execute
    raise HttpError(resp, content, uri=self.uri)
    googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/1RaeZ4QpT3-ZCcIfBDKdtQJt0WDJKiLnBsB3dTC2PoGg/values/Sheet2%21A1%3AA10:append?valueInputOption=RAW&alt=json returned "Request had insufficient authentication scopes.">

    Process finished with exit code 1

I pin a screenshot that prooves that i have all the permisions in the api. Google API Thank you in advance.

  • I thought that from your situation, the scopes in your script might not be reflected to the refresh token in "token.json". So how about this flow? 1. Delete "token.json". 2. Run the script and authorize again. By this, "token.json" including the refresh token reflected the scopes is created. If this was not your solution, I'm sorry. – Tanaike Aug 10 '18 at 00:31
  • Thank you Tanaike it worked! –  Aug 10 '18 at 07:58
  • Welcome. I'm glad your issue was solved. Thank you for trying and letting me know. – Tanaike Aug 10 '18 at 23:17

1 Answers1

1

From my understanding, exit code 1 "means there was some issue / problem which caused the program to exit." Trying to debug your code, it seems like your indentation is off. Everything between lines 11 and 25 should have at least one indent so it's considered to be apart of the main() function. Like so:

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools

SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file']

SAMPLE_SPREADSHEET_ID = 'ID'
SAMPLE_RANGE = 'Sheet2!A1:A10'

def main():
    store = oauth_file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)

    service = build('sheets', 'v4', http=creds.authorize(Http()))

    values = {'values': [['one','two','three']]}
    result = service.spreadsheets().values().append(
        spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE,
        valueInputOption='RAW',
        body=values).execute()
    print('{0} cells updated.'.format(result.get('updatedCells')));

if __name__ == '__main__':
    main()

Also, the Google Sheets API Quick Start for Python gets updated frequently, so I would try using the new code featured here. Let us know what happens after you fix the indentation or you decide to go with Google's updated code.

  • Hi Jordan. Thank you for your answer but it didn't help, it shows me the same errors. I checked errors again and it showed me that I have error 403 with insufficient authentication while in API dashboard it shows me that I have full access to all resources. –  Aug 09 '18 at 19:17