2012-08-15 100 views
2

我试图从javascript应用程序中获取电子表格文档的内容。但是'/drive/v2/files/' + documentId的响应没有“downloadUrl”属性。从Google Drive请求文件元数据时缺少'downloadUrl'参数

我的代码是:

var apiKey, clientId, documentId, scopes; 

documentId = "XXXXXXXXXX"; 
clientId = 'XXXXXXXXXXXXX.apps.googleusercontent.com'; 
apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXX'; 

scopes = 'https://www.googleapis.com/auth/drive'; 

window.OnLoadCallback = function() { 
    console.log("google client loaded!"); 
    gapi.client.setApiKey(apiKey); 
    return window.setTimeout(checkAuth, 1); 
}; 

window.checkAuth = function() { 
    return gapi.auth.authorize({ 
    client_id: clientId, 
    scope: scopes, 
    immediate: true 
    }, function(e) { 
    console.log("authorized!"); 
    return gapi.client.request({ 
     'path': '/drive/v2/files/' + documentId, 
     'method': 'GET', 
     callback: function(theResponseJS, theResponseTXT) { 
     var downloadUrl, myToken, myXHR; 
     console.log(theResponseJS); 
     console.log(theResponseJS.downloadUrl); //is missing 

     myToken = gapi.auth.getToken(); 
     myXHR = new XMLHttpRequest(); 
     myXHR.open('GET', theResponseJS.downloadUrl, true); 
     myXHR.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token); 
     myXHR.onreadystatechange = function(theProgressEvent) { 
      if (myXHR.readyState === 4) { 
      if (myXHR.status === 200) { 
       return console.log(myXHR.response); 
      } 
      } 
     }; 
     return myXHR.send(); 
     } 
    }); 
    }); 
}; 

我在做什么错?

谢谢!

回答

4

Google doc文件有一些内部/专有格式,似乎无法直接访问。唯一的选择是通过“exportLinks”集合将Google文档转换为其他定义良好的文档类型。

+0

我干了!结果为: 选项https://docs.google.com/feeds/download/spreadsheets/Export?key=XXXXX&exportFormat=ods 405(方法不允许) XMLHttpRequest无法加载https://docs.google.com/供稿/下载/表格/导出?关键= XXXXXXXXXXXXXX&exportFormat =消耗臭氧层物质。来源http://budget.t.proxylocal.com不被Access-Control-Allow-Origin允许。 – 2012-08-15 16:12:11

+0

不好意思格式化。你可以看到结果[这里](http://pastie.org/pastes/4494435/text) – 2012-08-15 16:19:04