簡體   English   中英

使用 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。 這樣做的網址是什么?

我知道我可以使用 GET API,但我正在嘗試使用簡單的 AJAX 而沒有 OAuth(文件是公開的)

回答:

雖然托管https://spreadsheets.google.com/feeds/cells/<SHEETS_ID>/1/public/full?alt=json端點的 Google Spreadsheets Data API 仍然有效,但 Google Docs 不是並獲得了不再可能以這種方式使用 JSON 格式的文檔。

更多信息:

您引用的用於獲取表格數據作為 JSON 結構的端點是 Google 數據 API 的一部分,並且是 Google 較舊的 API 之一 - 其中大部分已被較新的 API 取代。 正如您在此處看到的,電子表格一仍然有效,其中有一個交互式示例解釋如何形成此 URL。

但是,正如您在GData API Directory 文檔中所見,Google Documents List Data API 已被關閉並由 Google Drive API 取代。 不幸的是,這意味着您正在尋找的方法已被棄用,因此現在需要使用 Drive 或 Docs API。

我知道這通常是個壞消息,但我希望這對您有所幫助!

參考:

您可以在此處的Google Docs API 代碼示例中找到該信息。

具體來說,它是對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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM