使用 Google 文档作为数据端点来获取 JSON

Using Google docs as a data endpoint to get JSON

我已经成功地使用这个 URL 从 JSON 中的 Google 表格中提取数据:

https://spreadsheets.google.com/feeds/cells/<SHEETS_ID>/1/public/full?alt=json

我现在想获取 Google Docs 文档的 JSON。 URL 会是什么?

我知道我可以使用 GET API,但我正在尝试使用简单的 AJAX 并且没有 OAuth(文件是 public)

您可以在 Google 文档 API 代码示例 here 中找到该信息。

具体来说是对https://docs.googleapis.com/v1/documents/{documentId}.

GET请求

如果成功,响应主体包含一个 Document 的实例,您可以将其转换为 JSON。

Javascript中的示例:

<!DOCTYPE html>
<html>
  <head>
    <title>
      Docs API Extract Body
    </title>
    <meta charset="utf-8"/>
  </head>
  <body>
    <p>
      Docs API Extract Body
    </p>
    <!--Add buttons to initiate auth sequence and sign out-->
    <button id="authorize-button" style="display: none;">Authorize</button>
    <button id="signout-button" style="display: none;">Sign Out</button>
    <pre id="content"></pre>

    <script type="text/javascript">
      // Client ID and API key from the Developer Console
      var CLIENT_ID = '<YOUR_CLIENT_ID>'
      var API_KEY = '<YOUR_API_KEY>';

      // Array of API discovery doc URLs for APIs used by the sample
      var DISCOVERY_DOCS = [
        'https://docs.googleapis.com/$discovery/rest?version=v1'];

      // Authorization scopes required by the API; multiple scopes can be
      // included, separated by spaces.
      var SCOPES = "https://www.googleapis.com/auth/documents.readonly";

      var authorizeButton = document.getElementById('authorize-button');
      var signoutButton = document.getElementById('signout-button');

      /**
       *  On load, called to load the auth2 library and API client library.
       */
      function handleClientLoad() {
        gapi.load('client:auth2', initClient);
      }

      /**
       *  Initializes the API client library and sets up sign-in state
       *  listeners.
       */
      function initClient() {
        gapi.client.init({
        apiKey: API_KEY,
        clientId: CLIENT_ID,
        discoveryDocs: DISCOVERY_DOCS,
        scope: SCOPES
      }).then(function () {
        // Listen for sign-in state changes.
        gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);

      // Handle the initial sign-in state.
        updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
        authorizeButton.onclick = handleAuthClick;
        signoutButton.onclick = handleSignoutClick;
        });
      }

      /**
       *  Called when the signed in status changes, to update the UI
       *  appropriately. After a sign-in, the API is called.
       */
      function updateSigninStatus(isSignedIn) {
        if (isSignedIn) {
        authorizeButton.style.display = 'none';
        signoutButton.style.display = 'block';
        printDocBody();
        } else {
        authorizeButton.style.display = 'block';
        signoutButton.style.display = 'none';
        }
      }

      /**
       *  Sign in the user upon button click.
       */
      function handleAuthClick(event) {
        gapi.auth2.getAuthInstance().signIn();
      }

      /**
       *  Sign out the user upon button click.
       */
      function handleSignoutClick(event) {
        gapi.auth2.getAuthInstance().signOut();
      }

      /**
       * Append a pre element to the body containing the given message
       * as its text node. Used to display the results of the API call.
       *
       * @param {string} message Text to be placed in pre element.
       */
      function appendPre(message) {
        var pre = document.getElementById('content');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);
      }

      /**
       * Prints the JSON body of a document.
       */
      function printDocBody() {
        gapi.client.docs.documents.get({
        documentId: 'DOCUMENT_ID'
      }).then(function(response) {
        var doc = response.result;
        appendPre(JSON.stringify(doc.body, null, 4));
      },function(response) {
        appendPre('Error: ' + response.result.error.message);
        });
      }
    </script>
    <script async="" defer="" onload="this.onload=function(){};handleClientLoad()" onreadystatechange="if (this.readyState === 'complete') this.onload()" src="https://apis.google.com/js/api.js"></script>
  </body>
</html>

答案:

虽然托管 https://spreadsheets.google.com/feeds/cells/<SHEETS_ID>/1/public/full?alt=json 端点的 Google 电子表格数据 API 仍然存在,但 Google 文档不是并且正在获取JSON 格式的文档不再可用。

更多信息:

您引用以获取表格数据作为 JSON 结构的端点是 Google 数据 API 的一部分,并且是 Google 的旧数据之一APIs - 其中大部分已被更新的 APIs 取代。电子表格 as you can see here 仍然有效,其中有一个交互式示例解释了如何形成此 URL。

正如您在 GData API Directory documentation 中看到的那样,Google 文档列表数据 API 已关闭并被 Google 驱动器 API.不幸的是,这意味着您正在寻找的方法已被弃用,因此现在需要使用云端硬盘或文档 APIs。

参考文献: