Home › Forums › Mayfly Data Logger › Library alternatives to achieve same goal as dataLogger in Modular Sensors › Reply To: Library alternatives to achieve same goal as dataLogger in Modular Sensors
@cameronlight, I had a suggestion to try replit.com – enabled an account and it allows 10apps to be built free.
I tried “Create a curl script with Oauth2 access to post 3 values to google sheets”
I was logged into my google account, for latter testing. When asked to allow replit permissions pretty have to allow it all it seems, particularly read/write spreadsheets.
This was the script it created. I ended up being able to add 3 values to a spreadsheet in my account.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#!/bin/bash SPREADSHEET_ID="${1:-YOUR_SPREADSHEET_ID}" RANGE="${2:-Sheet1!A1:C1}" VALUE1="${3:-Hello}" VALUE2="${4:-World}" VALUE3="${5:-123}" get_access_token() { local hostname="$REPLIT_CONNECTORS_HOSTNAME" local x_replit_token="" if [ -n "$REPL_IDENTITY" ]; then x_replit_token="repl $REPL_IDENTITY" elif [ -n "$WEB_REPL_RENEWAL" ]; then x_replit_token="depl $WEB_REPL_RENEWAL" else echo "Error: No authentication token found" >&2 exit 1 fi local response=$(curl -s "https://${hostname}/api/v2/connection?include_secrets=true&connector_names=google-sheet" \ -H "Accept: application/json" \ -H "X_REPLIT_TOKEN: ${x_replit_token}") local access_token=$(echo "$response" | grep -o '"access_token":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -z "$access_token" ]; then echo "Error: Could not retrieve access token. Make sure Google Sheets is connected." >&2 echo "Response: $response" >&2 exit 1 fi echo "$access_token" } post_values_to_sheet() { local spreadsheet_id="$1" local range="$2" local val1="$3" local val2="$4" local val3="$5" local access_token="$6" local json_body=$(cat < [range] [value1] [value2] [value3]" echo "" echo "Examples:" echo " ./post_to_sheets.sh 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms" echo " ./post_to_sheets.sh 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms Sheet1!A1:C1" echo " ./post_to_sheets.sh 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms Sheet1!A1:C1 Apple Banana Cherry" echo "" echo "To find your Spreadsheet ID:" echo " Open your Google Sheet and look at the URL:" echo " https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/edit" echo " The SPREADSHEET_ID is the long string between /d/ and /edit" echo "" exit 0 fi echo "Retrieving OAuth2 access token..." ACCESS_TOKEN=$(get_access_token) if [ $? -ne 0 ]; then echo "Failed to get access token" exit 1 fi echo "Access token retrieved successfully." echo "" post_values_to_sheet "$SPREADSHEET_ID" "$RANGE" "$VALUE1" "$VALUE2" "$VALUE3" "$ACCESS_TOKEN" |
This is a script worked in the replit sandbox.
./post_to_sheets.sh <spreadsheet_id> [range] [value1] [value2] [value3]
or per the example
./post_to_sheets.sh 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms Sheet1!A1:C1 Apple Banana Cherry
There maybe some REPLIT secret sauce, as its in a bash file, it does identify program flow.
The next step would be to see if it can be made to work on local PC. !!
Welcome to EnviroDIY, a community for do-it-yourself environmental science and monitoring. EnviroDIY is part of