Как определить ключ API в google apis explorer? - PullRequest
1 голос
/ 05 августа 2020

Я просто тестирую API Google Sheets по адресу:

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

Я использую раздел Try this API для заполнения параметров запроса и выполните запрос (добавив строку к листу).

Я следил за Step 1 здесь:

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

По порядку на:

  • Включить API Google Таблиц
  • Создать ключ API

Таким образом, у меня есть:

  • Идентификатор клиента
  • Секрет клиента
  • Ключ API

В разделе Try this API> Credentials есть два флажка:

  • Google OAuth 2.0
  • API-ключ

Я попытался использовать uncheck параметр Google OAuth 2.0, чтобы я мог просто сделать запрос, используя API Key - однако я не вижу, где я можно ввести API Key.

введите описание изображения здесь

Как я могу определить ключ API в разделе Try this API, чтобы я мог делать запрос только с API Key (а не Google OAuth 2.0 ).

1 Ответ

2 голосов
/ 06 августа 2020

Как насчет этого ответа?

Проблема и решение:

К сожалению, на текущем этапе «Попробуйте этот API» нельзя напрямую использовать, вручную вводя ключ API и токен доступа. Но в этом случае есть обходной путь. См. Рисунок ниже.

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 здесь , здесь .

Ссылка:

...