2016-12-01 38 views
0

是否可以通過API下載,修改和上傳Google文檔的JSON表示?如何下載/上傳Google文檔的JSON格式?

我想寫一個服務器端應用程序來做到這一點。 Google文檔是指根據https://docs.google.com提供的富文本編輯功能。

據我所知,RealTime API應該允許我下載帶有GET請求的文檔的json表示,並上傳帶有PUT請求的新JSON文件。從文檔來看,這聽起來很理想。但是,來自GET請求的響應在數據字段中包含null。我明白這是因爲我的OAuth2.0 app is not the same app that created the document。如果我希望將文件視爲與任何其他Google文檔(如上所述)相同,我不確定是否/如何解決此問題。

Drive API允許我使用GET request下載文件,但是,支持的MIME類型不包含JSON。我知道我可以嘗試並轉換它們(例如,通過像優秀的pandoc這樣的庫),但是這需要有損和不可預知的處理來試圖猜測Google的文檔表示可能通過例如解析MS Word文檔(ew)。

有沒有辦法在Google自己的JSON表示中直接導入&導出文檔?

回答

0

您可能希望在鑑權模式,稱爲in-memory mode它允許你開始使用無需任何配置或登錄API的使用Realtime API嘗試。

要構建未經身份驗證的應用程序,您可以訪問並嘗試Google Realtime API Quickstart中給出的步驟。您可以簡單地將以下代碼複製到一個新文件中,然後在瀏覽器中打開它。

<!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> 

希望有幫助!

+0

嘿,非常感謝回覆。根據我的問題,我已經檢查過,但得出的結論是,雖然RealTime API可以用於處理自定義應用程序中的任意狀態(如您所示),但它不能用於編輯由「谷歌文檔「應用程序,在https://docs.google.com這個詞的意義上說,即谷歌等同於MS Word文檔。這是正確的,還是我錯過了什麼? –

相關問題