下面是一个成功验证的页面,然后尝试使用 drive.realtime.get 方法以三种方式获取现有实时文档的 JSON 导出。console.log 调用的结果显示在注释中。
id 为“EXISTING-FILE-ID”的文件存在,并且已使用实时 api 添加了内容。我可以在 https://www.googleapis.com/drive/v2/files/EXISTING-FILE-ID/realtime?access_token=VALID-ACCESS-TOKEN的浏览器中获取 JSON 导出的数据,它返回
{"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 的响应正文是一个字符串,当文档内容通过实时 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>