为什么对gapi.client.drive.realtime.get的响应为空?

时间:2013-08-01 17:45:24

标签: google-drive-api

下面是一个成功进行身份验证的页面,然后尝试使用drive.realtime.get方法以三种方式获取现有实时文档的JSON导出。 console.log调用的结果显示在注释中。

存在ID为'EXISTING-FILE-ID'的文件,并使用实时api添加了内容。我能够在浏览器中获取JSON导出的数据 https://www.googleapis.com/drive/v2/files/EXISTING-FILE-ID/realtime?access_token=VALID-ACCESS-TOKEN返回

{"appId":"CLIENT-ID","revision":10,"data":{"id":"root","type":"Map","value":{"blah":{"json":"anything"},"key":{"json":"val"},"key2":{"json":"val2"}}}}

但是,在Chrome,Firefox和Safari中,对gapi.client.drive.realtime.get和gapi.client.rpcRequest的响应始终为空:{"result":{}}

在Chrome和Firefox中,对gapi.client.request的响应正文是一串字符,当使用realtime api更改文档内容时,这些字符会发生部分更改。这可能是一些gzip压缩内容(响应头包括{content-encoding:“gzip”},但我无法对其进行压缩。响应头中的etag也会在文档更改时发生变化。

在Safari中,gapi.client.request响应主体包含与Chrome和Firefox(eyJH ...)相同的字符串,但导出文档的正确内容显示在控制台日志中,与我使用带有googleapis.com网址的浏览器窗口。

<!DOCTYPE html><html><head>
  <script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
  <script type="text/javascript">
    var fileId = 'EXISTING-FILE-ID';
    var start = function() {
      // load apis (then call authorize)
      gapi.load('auth:client,drive-realtime', function() {
        gapi.client.load('drive', 'v2', function() {
          authorize();
        });
      });
    };
    // authorize with drive scope
    var authorize = function() {
      gapi.auth.authorize({
        'client_id': 'CLIENT-ID',
        'scope': ['https://www.googleapis.com/auth/drive',
                  'openid'],
        'immediate': true
      }, function() {
        realtimeget(fileId);
      });
    };
    // try to get realtime document export in 3 different ways
    var realtimeget = function(id) {
      gapi.client.drive.realtime.get({
        'fileId': id
      }).execute(function() {
        console.log(JSON.stringify(arguments));
        // {"0":{"result":{}},"1":"[\n {\n  \"id\": \"gapiRpc\",\n  \"result\": {}\n }\n]\n"} 
      });
      gapi.client.rpcRequest('drive.realtime.get', 'v2', {
        'fileId': id
      }).execute(function() {
        console.log(JSON.stringify(arguments));
        // {"0":{"result":{}},"1":"[\n {\n  \"id\": \"gapiRpc\",\n  \"result\": {}\n }\n]\n"} 
      });
      gapi.client.request({
        'path': '/drive/v2/files/' + id + '/realtime',
        'method': 'GET',
      }).execute(function() {
        console.log('gapi.client.request:');
        console.log(arguments[0]);
        // false
        console.log(arguments[1]);
        // {"gapiRequest":{"data":{"body":"eyJhcHBJZCI6IjEwNjY4MTY3MjA5NzQiLCJyZXZpc2lvbiI6MTAsImRhdGEiOnsiaWQiOiJyb290IiwidHlwZSI6Ik1hcCIsInZhbHVlIjp7ImJsYWgiOnsianNvbiI6ImFueXRoaW5nIn0sImtleSI6eyJqc29uIjoidmFsIn0sImtleTIiOnsianNvbiI6InZhbDIifX19fQ==","headers":{"date":"Thu, 08 Aug 2013 19:17:19 GMT","content-encoding":"gzip","x-goog-safety-encoding":"base64","server":"GSE","etag":"\"Q5ElJByAJoL0etObruYVPRipH1k/fDOlc7uypufY3ROxh-RtfV86Kmg\"","content-type":"text/plain; charset=UTF-8","cache-control":"private, max-age=0, must-revalidate, no-transform","x-goog-safety-content-type":"application/json","content-length":"183","expires":"Thu, 08 Aug 2013 19:17:19 GMT"},"status":200,"statusText":"OK"}}} 
      });
    };
  </script>
</head>
<body onload="start();"></body></html>

2 个答案:

答案 0 :(得分:3)

我们正在调查客户端库的问题,但是现在我建议只对导出URL进行XHR GET:

var id = '{DOCUMENT ID}';
var accessToken = gapi.auth.getToken()['access_token'];
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.googleapis.com/drive/v2/files/' + id + '/realtime?access_token=' + accessToken);
xhr.onload = function() {
  console.log(xhr.responseText);
};
xhr.onerror = function() {
  // Handle error
};
xhr.send();

答案 1 :(得分:1)

如果您只是按原样运行此内联,我认为问题只是您需要等待内容保存才能获得。

在进行更改后向文档中添加DocumentSaveStateChangedEvent侦听器,并在isPending和isSaving都为false时触发实时时间。

查看此代码,单独的页面加载不会执行任何操作,因为它每次都会创建一个新文档。