Another part of my "science hive" project, is the charting of accumulated weight over time.
I'm logging the weight from 4 Stamps.com postage scales on a Raspberry Pi , to a text file, and then manually copying and pasting it into Excel when I want to update the chart .
This would be a perfect use for the Google Sheets API, but I can't figure it out. I followed this guide all the way through, and I can get through every step except for actually updating the sheet:
Thanks!
I'm logging the weight from 4 Stamps.com postage scales on a Raspberry Pi , to a text file, and then manually copying and pasting it into Excel when I want to update the chart .
This would be a perfect use for the Google Sheets API, but I can't figure it out. I followed this guide all the way through, and I can get through every step except for actually updating the sheet:
- I have a project, the API, OAuth creds, and a token
- The instructions had (I think) a minor error in that the URL to obtain a token:
-
Code:
https://accounts.google.com/o/oauth2/auth?client_id=[YOUR CLIENT-ID]&redirect_uri=http://localhost:3000&scope=https://www.googleapis.com/auth/drive.file
- returns "Required parameter is missing: response_type" so I took a guess and added "&response_type=code", and that returned:
-
Code:
http://localhost:3000/?code=<really long code>&scope=https://www.googleapis.com/auth/drive.file
-
- The instructions also seem a bit incomplete, in that I think she intends that we repeat that process for each of the Scopes in that list of URLs, e.g.
- so I did that repeatedly and recorded the returned codes, then used those codes to request access tokens.
- The instructions had (I think) a minor error in that the URL to obtain a token:
- His final step is to construct a cURL command to inject data into a sheet.
- I copied his example into InsertDataIntoGoogleSheet.sh, inserted my values, and ran it.
- My script returns
-
HTML:
<a href=//www.google.com/><span id=logo aria-label=Google></span></a> <p><b>404.</b> <ins>That’s an error.</ins> <p>The requested URL <code>/v4/spreadsheets/1mUJuULK20EPo6AOjhvBusi0FHVH6fWPX3_dmuvKwqHc/values/Sheet1!A1:C1</code> was not found on this server. <ins>That’s all we know.</ins>
- I don't care about privacy/security on that sheet, since it's just a dummy sheet for testing this data injection, and the sheet ID matches the sheet ID in the error.
-
- As a check, I inserted echo $ACCESS_TOKEN in his Line 20, just before construction of the cURL command to POST the data, and it does print out a long string that looks like a token, so I think that means I did everything above it correctly.
- Do his instructions look correct? Or is there a possibly syntax error in there?
Code:
#!/bin/bash
# Define Some variable values to insert
value1="Server01"
value2="/var/www"
value3="85% Used"
# Set the spreadsheet ID and range
SPREADSHEET_ID="1w5TSDYt6Cbg8-_liMWDgTxgcSAb8JdpbPVTnzfj7E1I"
RANGE="Sheet1!A1:C1"
# Set the client ID and client secret
CLIENT_ID="251327740099-6667am9lnhjeffc22cequ1gdo1uchcbb.apps.googleusercontent.com"
CLIENT_SECRET="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"
REFRESH_TOKEN="1//0gg3yvsZe2NNVCgYIARAAGBASNwF-L9IrLiIg4_bSFdSBjz9OBrewqMMDHJWd1NNjUc_Kkw_O7MfJUXPHAJySNmK44KHD0KYy9o0"
# Construct cURL command to get Access Token
AUTH_RESPONSE=$(curl --request POST --data "access_type=offline&refresh_token=$REFRESH_TOKEN&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&grant_type=refresh_token" https://oauth2.googleapis.com/token)
ACCESS_TOKEN=$(echo $AUTH_RESPONSE | jq -r '.access_token')
# Send the request to Google Sheets API
curl -s -X POST \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"values\": [[\"$value1\", \"$value2\", \"$value3\"]]}" \
"https://sheets.googleapis.com/v4/spreadsheets/$SPREADSHEET_ID/values/$RANGE:append?insertDataOption=INSERT_ROWS&valueInputOption=USER_ENTERED"
Thanks!