Google Sheets with Python

0

I'm trying to upload some data to Google spreadsheets with Python. For this I use in module requests. I have managed to read a spreadsheet without problem using this code:

import requests 
import json

url = "https://sheets.googleapis.com/v4/spreadsheets/xxxxxxxxxxxxxxx/values/Hoja1!A1:B1?key=xxxxxxxxxxx"

r = requests.get(url)

results = r.json()

print (results['values'])

To read that spreadsheet I only use the spreadsheet id and a key.

Now when I go to edit that spreadsheet I have an authentication problem. The code I use is the one:

url = "https://sheets.googleapis.com/v4/spreadsheets/xxxxxxxxxxxx/values/Hoja1!A1:B1?valueInputOption=USER_ENTERED?key=xxxxxxxxxxxxxx"

r = requests.put(url, data=data )

results = r.json()

The code returns an authentication error:

  

{u'error ': {u'status': u'UNAUTHENTICATED ', u'message': u'Request is   missing required authentication credential. Expected OAuth 2 access   token, login cookie or other valid authentication credential. See    link . ',   u'code ': 401}}

Apparently, to edit the spreadsheets, something more than the id of the spreadsheet and the key is needed. Entering the error link I was able to create a Google project and download some credentials to access the edition. I have the credentials in a file called "credentials.json".

What I do not know is how to include it in the order of requests to proceed with the authentication.

I have to say that unfortunately I can not use any library like gspread or Google, because it has to work in an ESP32 with MicroPython and these libraries are not available. That's why I have to use the requests module, which is almost identical in Python and MicroPython.

If anyone knows how I can edit the Google sheets using the requests module, I would be very grateful, but I can also use any other method that can be used with MicroPython.

Thanks in advance.

    
asked by Andermutu 15.11.2018 в 12:23
source

0 answers