如何下载/上传Google文档的JSON表示形式? [英] How to download / upload the JSON representation of a Google doc?

查看:58
本文介绍了如何下载/上传Google文档的JSON表示形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过API下载,修改和上传Google文档的JSON表示形式?

Is it possible to download, modify, and upload the JSON representation of a Google doc via an API?

我正在尝试编写服务器端应用程序来执行此操作.对于Google文档,我的意思是根据 https://docs.google.com >.

I'm trying to write a server side app to do this. By Google doc, I mean files underlying the rich-text editing features as per https://docs.google.com.

据我了解,RealTime API应该允许我通过GET请求下载文档的json表示形式,并通过PUT请求上传新的JSON文件.从文档中听起来很理想.但是,来自GET请求的响应在数据字段中包含 null .我了解这是因为我的OAuth2.0 应用是不是创建文档的同一应用.如果我希望将文件与其他任何Google文档(如上定义)一样对待,我不确定是否/如何解决此问题.

As far as I've understood, the RealTime API should allow me to download the json representation of a doc with a GET request, and upload a new JSON file with a PUT request. From the documentation it sounds ideal. However, responses from GET requests contain null in the data field. I understand that this is because my OAuth2.0 app is not the same app that created the document. I'm not sure if/how I could fix this if I want the files to be treated the same as any other Google doc (as defined above).

Drive API允许我下载带有 GET请求的文件,但是,受支持的mime类型不包含JSON.我知道我可以尝试将其转换(例如,通过诸如出色的pandoc之类的库),但这需要进行有损且不可预测的处理,才能尝试通过(例如)猜测Google的文档表示形式解析MS Word文档(ew).

The Drive API allows me to download a file with a GET request, however, the supported mime-types do not include JSON. I am aware that I could try and convert them (e.g. via a library like the excellent pandoc) but this require lossy and unpredictable processing to try to guess at what Google's document representation might be via e.g. parsing MS Word documents (ew).

是否可以直接导入&以Google自己的JSON表示格式导出文档?

Is there a way to directly import & export docs in Google's own JSON representation?

推荐答案

您可能想尝试使用实时API 未经身份验证的模式,称为 内存模式 ,可让您无需任何配置或登录即可开始使用API​​.

You may want to try using the Realtime API in an unauthenticated mode, called in-memory mode which allows you to get started with the API without any configuration or login.

要构建未经身份验证的应用,您可以访问并尝试Google Realtime API快速入门.您只需将以下代码复制到一个新文件中,然后在浏览器中打开它即可.

To build An Unauthenticated App, you may visit and try the steps given in Google Realtime API Quickstart. You can simply copy the following code into a new file and then open it in a browser.

<!DOCTYPE html>
<html>
  <head>
    <title>Google Realtime Quickstart</title>

    <!-- Load Styles -->
    <link href="https://www.gstatic.com/realtime/quickstart-styles.css" rel="stylesheet" type="text/css"/>

    <!-- Load the Realtime API JavaScript library -->
    <script src="https://apis.google.com/js/api.js"></script>
  </head>
  <body>
    <main>
      <h1>Realtime Collaboration Quickstart</h1>
      <p>Welcome to the quickstart in-memory app!</p>
      <textarea id="text_area_1"></textarea>
      <textarea id="text_area_2"></textarea>
      <p>This document only exists in memory, so it doesn't have real-time collaboration enabled. However, you can persist it to your own disk using the model.toJson() function and load it using the model.loadFromJson() function. This enables your users without Google accounts to use your application.</p>
      <textarea id="json_textarea"></textarea>
      <button id="json_button" class="visible">GetJson</button>
    </main>
    <script>
      // Load the Realtime API, no auth needed.
      window.gapi.load('auth:client,drive-realtime,drive-share', start);

      function start() {
        var doc = gapi.drive.realtime.newInMemoryDocument();
        var model = doc.getModel();
        var collaborativeString = model.createString();
        collaborativeString.setText('Welcome to the Quickstart App!');
        model.getRoot().set('demo_string', collaborativeString);
        wireTextBoxes(collaborativeString);
        document.getElementById('json_button').addEventListener('click', function(){
          document.getElementById('json_textarea').value = model.toJson();
        });
      }

      // Connects the text boxes to the collaborative string.
      function wireTextBoxes(collaborativeString) {
        var textArea1 = document.getElementById('text_area_1');
        var textArea2 = document.getElementById('text_area_2');
        gapi.drive.realtime.databinding.bindString(collaborativeString, textArea1);
        gapi.drive.realtime.databinding.bindString(collaborativeString, textArea2);
      }
    </script>
  </body>
</html>

希望有帮助!

这篇关于如何下载/上传Google文档的JSON表示形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆