2
votes

I am just testing out the Google Sheets API at:

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append

I'm using the Try this API section to fill in the request parameters and execute the request (appending a row to a sheet).

I have followed Step 1 here:

https://developers.google.com/sheets/api/quickstart/js

In order to:

  • Enable Google Sheets API
  • Create an API Key

I therefore have:

  • Client ID
  • Client Secret
  • API Key

In the Try this API > Credentials section, there are two checkboxes:

  • Google OAuth 2.0
  • API key

I tried to uncheck the Google OAuth 2.0 option so that i could just make the request using the API Key - however I cannot see where I can enter the API Key.

enter image description here

How can I define the API Key in the Try this API section so that I can make a request with only the API Key (and not Google OAuth 2.0 ).

2

2 Answers

5
votes

How about this answer?

Issue and workaround:

Unfortunately, in the current stage, "Try this API" cannot be directly used by manually inputting the API key and access token. But in this case, there is a workaround. Please see the below figure.

enter image description here

This is the window of "Try this API". When you see this, you can see the clickable square box at the upper right as shown in the figure. Please click it. By this, you can see the expanded "Try this API" as following figure.

enter image description here

Here, please input the required parameters you want to use. By this, you can see the curl sample as follows.

curl --request POST \
  'https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEETID/values/RANGE:append?valueInputOption=USER_ENTERED&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"values":[["sample value"]]}' \
  --compressed

By using this command, you can use the API by manually inputting the API key and access token.

But, here, there is an important point. At Google API, the API key cannot be used for the method except for GET method. The API key can use only GET method for the publicly shared contents. Please be careful this. So when you want to use "Method: spreadsheets.values.append" in Sheets API by manually inputting the API key and the access token, please use the access token. Because this method uses the POST method. By this, you can test the API. Also, you can see the required scopes at the official document.

By the way, when you want to use only the API key, you are not required to use --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]'. And also, when you want to use only the access token, you are not required to use &key=[YOUR_API_KEY].

At the expanded "Try this API", you can also see HTTP request and the sample script of Javascript there.

Note:

  • When you want to request the Sheets API using the API key, please publicly share the Spreadsheet. By this, the value can be retrieved using the GET method like "Method: spreadsheets.values.get", "Method: spreadsheets.get" and so on.
  • When you want to see the method for retrieving the access token, you can see it at the official documents like here, here.

Reference:

1
votes

I think the correct answer to this question is: It is not possible to use the API Explorer to actually send a request to a Google API if that API requires any sort of API key. The Explorer appears to be for creating requests that you then copy and use in your code. There does not appear to be any way to add the API key (or anything else) directly to the Explorer interface.

This all begs the question of why the Explorer has an "EXECUTE" button if you cannot execute anything (except perhaps API requests that do not require any sort of access credentials).

It is bizarre. I would love to be proven wrong, but I don't think I am.

enter image description here