8

I've read google spreadsheet documentation https://developers.google.com/sheets/api/guides/authorizing And it says, that if document is public, you don't need Oauth 2.0 and API key is sufficient. I'm trying to do a test request with hurl and api key as parameter.it, but it still gives me error, that I need to use Oauth, any thoughts?

POST https://sheets.googleapis.com/v4/spreadsheets/16woR-nfy6KYBbkQpC2YOT1GzIean8rTzjueUnlzLMiE/values/Sheet1!A1:E1:append?valueInputOption=USER_ENTERED

Response: "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"}

sanevys
  • 559
  • 1
  • 7
  • 27
  • expected oauth OR login cookie OR other valid auth cred? – L_Church Feb 12 '18 at 13:25
  • @L_Church yes, maybe you know what I should do to use API key instead of Oauth? I already made sheet public – sanevys Feb 12 '18 at 13:28
  • that just looks like a generic error to me. it says it expects oauth but that doesnt mean you need that one. it says you can use login cookie or other creds so just scrounge around for anything else you can pass in (maybe api key idk) – L_Church Feb 12 '18 at 13:33
  • Will this information be useful for you? https://stackoverflow.com/questions/48412622/api-key-and-discovered-google-sheets-functions/48413284#48413284 – Tanaike Feb 12 '18 at 23:54

3 Answers3

8

You could use the format as below:

https://docs.google.com/spreadsheets/d/{sheetID}/export?format=csv

Make a URLConnection to this URL, after replacing the sheetID with your public sheet and read the csv file as you would normally do in your programming language of choice.

Please note that this is only true for Readable Public Google spreadsheets, and is a bit of a hack when you don't necessarily need to do the Credentials jig and jive

Nishant
  • 395
  • 4
  • 6
5

API key is not enough. You're trying to use spreadsheets.values.append which requires OAuth authorization:

Authorization Requires one of the following OAuth scopes:

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/spreadsheets
For more information, see the [Auth Guide](https://developers.google.com/identity/protocols/OAuth2).

Note it says OAuth.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • 1
    Yeah, I came up to that conclusion after two days of struggling :D. But thanks for clarifying – sanevys Feb 13 '18 at 14:31
3

In my case I need to publish content from a Google Spreadsheet to a web page. My workaround consists in use the HTTP GET call

GET https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}

using the javascript object XMLHttpRequest In this solution you only have to use the API key

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459