2012-08-15 104 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