HTTP 401 error for appending data to public Google Sheets using API Key with v4 API

I am facing an issue similar to another question regarding fetching spreadsheet data, but instead, I need to add data to a Google Sheet. Below is the curl command I am using:

curl -H "Content-Type: application/json" -X POST -d '{"range":"A1","majorDimension":"ROWS","values":[["Frank2"]]}' https://sheets.googleapis.com/v4/spreadsheets/{YOUR_SPREADSHEET_ID}/values/A1:append?valueInputOption=RAW&key={YOUR_API_KEY}

The response I am getting is:

{
  "error": {
    "code": 401,
    "message": "The request does not have valid authentication credentials.",
    "status": "UNAUTHENTICATED"
  }
}

Is it feasible to achieve this only with an API Key, or am I possibly making an error? The documentation indicates that it should work if the spreadsheet is publicly accessible.

Based on your situation, it seems that the issue might be related to the type of access required for writing data. While API keys are sufficient for reading public data, writing data typically requires OAuth 2.0 authorization. This is due to Google needing to ensure that enacted changes are performed by an authenticated user, even when sheets are public. In practical terms, you’ll need to set up an OAuth 2.0 client in the Google Cloud Platform Console, obtain an access token, and use it in your requests to modify the Google Sheet data.