Javascript 使用GoogleDocs作为获取JSON的数据端点

Javascript 使用GoogleDocs作为获取JSON的数据端点,javascript,json,google-docs-api,Javascript,Json,Google Docs Api,我已经成功地使用此URL以JSON格式从Google工作表中提取数据: https://spreadsheets.google.com/feeds/cells//1/public/full?alt=json 我想现在就获取Google文档的JSON。URL会做什么呢 我知道我可以使用GETAPI,但我正在尝试使用简单的AJAX和非OAuth(文件是公共的)您可以在Google Docs API代码示例中找到这些信息 具体而言,这是一个请求https://docs.googleapis.com/v

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

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

我想现在就获取Google文档的JSON。URL会做什么呢


我知道我可以使用GETAPI,但我正在尝试使用简单的AJAX和非OAuth(文件是公共的)

您可以在Google Docs API代码示例中找到这些信息

具体而言,这是一个请求
https://docs.googleapis.com/v1/documents/{documentId}

如果成功,响应主体将包含一个
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>

Docs API提取体

Docs API提取体

授权 退出 //来自开发人员控制台的客户端ID和API密钥 变量客户端\u ID=“” var API_键=“”; //样本使用的API的API发现文档URL数组 变量发现\u文档=[ 'https://docs.googleapis.com/$discovery/rest?版本=v1']; //API要求的授权范围;可以使用多个作用域 //包含,用空格分隔。 变量作用域=”https://www.googleapis.com/auth/documents.readonly"; var authorizeButton=document.getElementById('authorized-button'); var signburatton=document.getElementById('signout-button'); /** *加载时,调用以加载auth2库和API客户端库。 */ 函数handleClientLoad(){ load('client:auth2',initClient); } /** *初始化API客户端库并设置登录状态 *听众。 */ 函数initClient(){ gapi.client.init({ API密钥:API_密钥, clientId:客户端ID, 发现文档:发现文档, 范围:范围 }).然后(函数(){ //侦听登录状态的更改。 gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus); //处理初始登录状态。 updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get()); authorizeButton.onclick=handleAuthClick; SignerButton.onclick=handleSignoutClick; }); } /** *当登录状态更改时调用,以更新UI *适当地。登录后,将调用API。 */ 函数updateSigninStatus(isSignedIn){ 如果(伊西涅丁){ authorizeButton.style.display='none'; signexputton.style.display='block'; printDocBody(); }否则{ authorizeButton.style.display='block'; signexputton.style.display='none'; } } /** *单击按钮后登录用户。 */ 函数handleAuthClick(事件){ gapi.auth2.getAuthInstance().signIn(); } /** *单击按钮后注销用户。 */ 函数handleSignoutClick(事件){ gapi.auth2.getAuthInstance().signOut(); } /** *将pre元素附加到包含给定消息的正文中 *作为其文本节点。用于显示API调用的结果。 * *@param{string}要放置在pre元素中的消息文本。 */ 函数appendPre(消息){ var pre=document.getElementById('content'); var textContent=document.createTextNode(message+'\n'); pre.appendChild(文本内容); } /** *打印文档的JSON正文。 */ 函数printDocBody(){ gapi.client.docs.documents.get({ 文档ID:“文档\u ID” }).然后(功能(响应){ var doc=response.result; appendPre(JSON.stringify(doc.body,null,4)); },功能(回应){ appendPre('Error:'+response.result.Error.message); }); }
回答: 而承载
https://spreadsheets.google.com/feeds/cells//1/public/full?alt=json
endpoint仍然有效,而Google Docs one则不存在,因此无法再以这种方式获取JSON格式的文档

更多信息: 作为JSON结构获取图纸数据所引用的端点是Google数据API的一部分,是Google较旧的API之一,其中大部分已被较新的API所取代。电子表格之一仍然是实时的,其中有一个交互式示例解释如何形成此URL

但是,正如您在中所看到的,Google文档列表数据API已经关闭,并被Google驱动器API取代。不幸的是,这意味着您正在寻找的方法已被弃用,因此现在需要使用驱动器或docsapi

我知道这通常是个坏消息,但我希望这对你有帮助

参考资料:

是的。但是我想绕过OAuth过程,能够直接从公共文档中提取JSON,就像我能够处理带有此URL的表单一样:我可以问一下您需要的Google文档的值吗?文档的文本、页面样式等。因为GoogleDocsAPI不同于GoogleSheetsAPI,首先,我想确认一下。