Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Google Sheets v4 API and DHT sensor readings to Gdocs
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Please be positive and constructive with your questions and comments.

Google Sheets v4 API and DHT sensor readings to Gdocs

by Katoen1958 on Fri Apr 23, 2021 1:44 pm

Hello,

you made a nice tutorial https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging on getting readings from the DHT sensor to Google-docs through the Raspberry Pi. I use it for several years now. Recently I got an email from Google Workspace with the title: Update projects in your domain to Google Sheets v4 API by June 8, 2021. They named the project that I use for above DHT-project to transfer it from Google Sheets v3 API to v4 API. I tried to figure out what I have to change in the code you provided, the only thing I found is that oauth2client is deprecated (https://github.com/googleapis/oauth2client/releases and has to be replaced by google-auth and oauthlib. But I got lost in how to do that.
Or is it so simple by just upgrading Gspread with <pip install --upgrade gspread>, so I get gspread 3.7.0 wich supports Google Sheets v4 API?

I hope you have an answer.

Yours sincerely,
Bart Elffers, Amsterdam.



Total mail from Google Workspace:
Google Sheets v3 API will no longer be available as of June 8, 2021. Migrate projects to Google Sheets v4 before the shut down.
Dear Google Sheets Developer,

In September 2019, we announced that we’d shut down Google Sheets v3 API as part of the migration to Google Sheets v4 API. In our prior communications with you we announced that this API would be shut down on January 26. 2021, however, to allow you time to make your changes, we’ve extended this date to June 8, 2021.

Transition affected projects in your domain (listed below) to Sheets v4 API before June 8 to ensure they continue to work. If you don’t take any action, your projects will stop working.

What do I need to know?

This migration guide includes a map of Sheets v3 API operations to those in Sheets v4. It also presents examples of using the new functionality to your advantage.

Note: Sheets v4 API doesn’t provide a List spreadsheets for the authenticated user operation, but it can be replicated using the Drive API Files.list method.

What do I need to do?

To ensure your projects built on the Sheets v3 API continue working, migrate all projects to the Sheets v4 API before June 8, 2021.

If you do nothing, your projects will stop working.

We’ve identified your following projects have used the Sheets v3 API in the past 30 days:

raspi-gspread-test (raspi-gspread-test)

How can I get help?

Review this documentation to get started using v4 Sheets on a new project, or to leverage content and appearance controls not available in previous versions.

Thanks for choosing Google Workspace.

—The Google Workspace Team
Katoen1958
 
Posts: 27
Joined: Wed Mar 20, 2013 4:40 am

Re: Google Sheets v4 API and DHT sensor readings to Gdocs

by Katoen1958 on Sun Apr 25, 2021 3:52 am

Hi guys,

reading the google instructions better it differs from a few years ago, so I wil try out that first, perhaps that's solving my problem.. Thanks anyway!

Bart Elffers,
Amsterdam.
Katoen1958
 
Posts: 27
Joined: Wed Mar 20, 2013 4:40 am

Re: Google Sheets v4 API and DHT sensor readings to Gdocs

by Katoen1958 on Mon Apr 26, 2021 3:01 am

Hi Guys,

reading the instructions better in https://gspread.readthedocs.io/en/latest/oauth2.html#enable-api-access, found they use other commands than you use. They use:
Code: Select all | TOGGLE FULL SIZE
from google.oauth2.service_account import Credentials

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

credentials = Credentials.from_service_account_file(
    'path/to/the/downloaded/file.json',
    scopes=scopes
)

gc = gspread.authorize(credentials)


You use:
Code: Select all | TOGGLE FULL SIZE
import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope =  ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(oauth_key_file, scope)
gc = gspread.authorize(credentials)


They say this in a note,
Note

Older versions of gspread have used oauth2client. Google has deprecated it in favor of google-auth. If you’re still using oauth2client credentials, the library will convert these to google-auth for you, but you can change your code to use the new credentials to make sure nothing breaks in the future.

So perhaps it's time to change this before June 8, 2021.

I hope to hear from you.

Kind regards,

Bart Elffers,
Amsterdam.
Katoen1958
 
Posts: 27
Joined: Wed Mar 20, 2013 4:40 am

Re: Google Sheets v4 API and DHT sensor readings to Gdocs

by Katoen1958 on Tue Apr 27, 2021 2:30 am

hi guys,

I think I managed the problem by myself!

I like to propose some changes with the depreciation of oauth2client by Google and Google Sheets API v3>v4..

installing gspread:
sudo pip3 install gspread google-auth pyasn1 pyasn1-modules

python script google_spreadsheet.py:
line 33:
from google.oauth2.service_account import Credentials

line 66:
GDOCS_OAUTH_JSON can be deleted.

line 75-80:
def login_open_sheet(spreadsheet): (oauth_key_file can be deleted)
try:
scopes = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
credentials = Credentials.from_service_account_file('path/to/the/downloaded/file.json',scopes=scopes)
gc = gspread.authorize(credentials)

line 96-97:
if worksheet is None:
worksheet = login_open_sheet(GDOCS_SPREADSHEET_NAME) GDOCS_OAUTH_JSON can be deleted

Thanks!!!
Bart.
Katoen1958
 
Posts: 27
Joined: Wed Mar 20, 2013 4:40 am

Please be positive and constructive with your questions and comments.