3

After some confusion, I did get reading from a public sheet working:

curl https://sheets.googleapis.com/v4/spreadsheets/[sheetID]/values/A1?key=[MyAPIKey]

Where Sheet ID is from google sheets, and [MyAPIKey] is from https://developers.google.com/sheets/api/guides/authorizing#APIKey

It gives me read-access to the sheet.

As soon as I try to write to any cells on the sheet (despite them being publicly writable) it gives me an error. I can easily edit them anonymously from my browser without any keys, however, I can't seem to edit them at all without API keys.

If I issue:

    curl https://sheets.googleapis.com/v4/spreadsheets/[sheetid]/values/Sheet1\!A1?valueInputOption=RAW?key=[MyAPIKey] -X PUT -d "{\"values\":[ [ \"Test\" ] ] }"

I receive: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "status": "UNAUTHENTICATED" } }

I think this is related to Google Sheets API v4 append request receives HTTP 401 response for public feeds using API Key

I'm avoiding the OAUTH2 mechanism as for this task, I can't allow applications access to entire google drive. How can this be done with selective link-based access, like is done via the authkey approach? Is there some other way to give sheet-at-a-time access in OAUTH2?

Community
  • 1
  • 1
Charles Lohr
  • 695
  • 1
  • 8
  • 23
  • 1
    User Authorization header with curl such as , curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Accept: application/json', 'Content-Type: application/json', 'Authorization: Bearer '.$token) ); Also ensure that the token u passed has valid scope – Altanai Oct 22 '18 at 10:06
  • 1
    Related: https://stackoverflow.com/questions/46583052/http-google-sheets-api-v4-how-to-access-without-oauth-2-0/46583300 – 425nesp Apr 07 '21 at 01:51

1 Answers1

-4

Nope, you can't perform Sheet operations using CURL alone. Error 401 means you need access tokens to work on that sheet. So you need to use OAuth flow to get rid of that error and also make sure to define the scope.

There's a bunch of quickstarts below the Introduction to Sheets that you can use.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • 1
    It's actually pretty easy to use cURL, alone with google sheets with the OAuth stuff (reading, writing, etc). I was actively trying to find another way because it doesn't fit well with the model of operations I'm trying to work with. I guess I'm just going to have to deal with doing it the OAuth path and create new gmail addresses for each of the sub-apps, then share the docs with those puppet users? – Charles Lohr Apr 26 '17 at 16:27
  • 1
    @CharlesLohr could you please publish an example of use Google API with the OAuth via curl? Thank you. – BrilliantContract Apr 16 '21 at 18:42
  • I regret I no longer know where the files associated with this project are located. – Charles Lohr Apr 21 '21 at 22:38