【问题标题】:Using Google docs as a data endpoint to get JSON使用 Google 文档作为数据端点来获取 JSON
【发布时间】:2020-01-19 12:33:24
【问题描述】:

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

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

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

我知道我可以使用 GET API,但我正在尝试使用简单的 AJAX 并且没有 OAuth(文件是公开的)来做到这一点

【问题讨论】:

  • 我能问一下您需要的 Google Document 的值吗?文档的文本、页面样式等。由于 Google Docs API 与 Google Sheets API 不同,首先我想确认一下。

标签: javascript json google-docs-api


【解决方案1】:

答案:

虽然托管 https://spreadsheets.google.com/feeds/cells/<SHEETS_ID>/1/public/full?alt=json 端点的 Google 电子表格数据 API 仍然有效,但 Google Docs 却没有,并且无法再以这种方式获取 JSON 格式的文档。 p>

更多信息:

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

正如您在GData API Directory documentation 中看到的那样,Google Documents List Data API 已被关闭并被 Google Drive API 取代。不幸的是,这意味着您正在寻找的方法已被弃用,因此现在需要使用 Drive 或 Docs API。

参考资料:

【讨论】:

    【解决方案2】:

    您可以在 Google Docs 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>
    

    【讨论】:

    猜你喜欢
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2022-11-26
    • 2014-06-05
    相关资源
    最近更新 更多