8

I try to use example "Read multiple ranges":

https://sheets.googleapis.com/v4/spreadsheets/{SpreadsheetID}/values:batchGet?ranges=Sheet1!B:B&ranges=Sheet1!D:D&valueRenderOption=UNFORMATTED_VALUES?majorDimension=COLUMNS

from: Google Sheets API v4 example I replaced "spreadsheetId" with my spreadsheet id. But when I send GET request (by Postman) I receive:

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "status": "PERMISSION_DENIED"
  }
}

Than I used https://developers.google.com/oauthplayground/ I made authorisation for Spreadsheet API v4 with my email and at the end I generated a long link with key and saw correct response with content of my data table.

My question is next: Why it's imposible to use the example from Google API (https://developers.google.com/sheets/api/samples/reading) only replace the spreadsheet id. P.s Link to my spreadsheet is public, My spreadsheet is public and available in JSON format and what is correct use of url for Google Spreadsheet API v4 in order to send batchGet with multiple requests?

P.s.s I use another approach and it works but why first approach (v4) doesn't work:

https://spreadsheets.google.com/feeds/list/{SpreadsheetID}/od6/public/values?alt=json

When I tried to combine 2 APIs in one request and received and error "Inconsistent repeating query parameter ranges":

https://spreadsheets.google.com/feeds/list/{SpreadsheetID}/od6/public/values:batchGet?ranges=Sheet1!B:B&ranges=Sheet1!D:D&valueRenderOption=UNFORMATTED_VALUES?majorDimension=COLUMNS

======================================
Edited:

SpreadSheetID: 1KBk1J7TJCwnayMnTbaysqvFk98kVDdxXWNw2JYc3bW0

Available in incognito mode: Spreadsheet

In JSON format: JSON

Reference: Google Sheets API v4: Read multiple ranges

The same example with SpreadSheetID: ERROR 403

Alex Pilugin
  • 683
  • 2
  • 10
  • 38
  • This is a link for example: [Data Table](https://docs.google.com/spreadsheets/d/1KBk1J7TJCwnayMnTbaysqvFk98kVDdxXWNw2JYc3bW0/pubhtml) The same spreadsheet in JSON: [json](https://spreadsheets.google.com/feeds/list/1KBk1J7TJCwnayMnTbaysqvFk98kVDdxXWNw2JYc3bW0/od6/public/values?alt=json) API v4: [error 403](https://sheets.googleapis.com/v4/spreadsheets/1KBk1J7TJCwnayMnTbaysqvFk98kVDdxXWNw2JYc3bW0/values:batchGet?ranges=%27Cost%20Data%27!B:B&ranges=%27Cost%20Data%27!D:F&valueRenderOption=UNFORMATTED_VALUES?majorDimension=COLUMNS) – Alex Pilugin Jun 26 '17 at 19:24
  • If you were authenticating using JWT/OAuth 2.0 with a service account then you'll need to make sure you share the specific sheet with the service account email – Sarah Dwyer Oct 17 '18 at 21:55

4 Answers4

8

I believe you are missing API in the url. This is from google spreadsheet api doc.

To acquire an API key:

Open the Credentials page in the API Console.

API keys: A request that does not provide an OAuth 2.0 token must send an API key. The key identifies your project and provides API access, quota, and reports.

The API supports several types of restrictions on API keys. If the API key that you need doesn't already exist, then create an API key in the Console by clicking Create credentials > API key. You can restrict the key before using it in production by clicking Restrict key and selecting one of the Restrictions. To keep your API keys secure, follow the best practices for securely using API keys.

After you have an API key, your application can append the query parameter key=yourAPIKey to all request URLs.

The API key is safe for embedding in URLs; it doesn't need any encoding.

Google Spreadsheet Docs

skamble89
  • 131
  • 1
  • 4
  • 4
    - I have read it. - Why do I need to use OAuth 2.0 if my spreadsheet is visible in Incognito mode in a browser? - Also the spreadsheet available through this url: https://spreadsheets.google.com/feeds/list/{SpreadsheetID}/od6/public/values?alt=json - Why Google API reference (example) has misleading examples? - The main issue for me: to use "batchGet" command. – Alex Pilugin Jun 26 '17 at 19:04
7

You have to make sure to pass in the key=YOUR_API_KEY as the request parameter. For example:

https://sheets.googleapis.com/v4/spreadsheets/{sheet_id}/values/Sheet1!A1:D5?key={YOUR_API_KEY}
Elijah
  • 1,814
  • 21
  • 27
2

In addition to @skamble89's answer, this 403 error is usually caused by incorrect or missed some configuration in Authorizing Requests. If you're authorizing requests with OAuth 2.0, make sure you use the proper scope with it.

Make sure you have a permission to call this spreadsheet and you enable the Sheets API in your developer console.

abielita
  • 13,147
  • 2
  • 17
  • 59
1
curl "https://sheets.googleapis.com/v4/spreadsheets/1ShTP408LPzLOGjqXQTRcmXXXXXXXXXXXXXPUteQmY7o/values/A1%3AD3?key={YourApy"