2
votes

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?

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 scopeAltanai

1 Answers

-1
votes

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.