0

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

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.

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).

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).

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

Community
  • 1
  • 1

1 Answers1

0

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.

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>

Hope that helps!

Teyam
  • 7,686
  • 3
  • 15
  • 22
  • Hey, thanks so much for responding. As per my question, I have checked this out before, but came to the conclusion that, while the RealTime API can be used to manipulate arbitrary state in custom applications (as you illustrate), it cannot be used to edit files created by the "Google Docs" app, in the https://docs.google.com sense of the word, i.e. Google equivalent of MS Word documents. Is this correct, or am I missing something? – computer_guy Dec 02 '16 at 23:16