2012-07-03 281 views
13

我試圖使用Google驅動器來列出文件。Google Drive API javascript

使用https://stackoverflow.com/a/11280257中的答案我發現了一個問題,我找不到原因。

var clientId = '*********.apps.googleusercontent.com'; 
var apiKey = '##########'; 
var scopes = 'https://www.googleapis.com/auth/drive'; 


function handleClientLoad() { 
    gapi.client.setApiKey(apiKey); 
    window.setTimeout(checkAuth,1); 
} 

function checkAuth() { 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true},handleAuthResult); 
} 

function handleAuthResult(authResult) { 
    var authorizeButton = document.getElementById('authorize-button'); 

    if (authResult && !authResult.error) { 
     authorizeButton.style.visibility = 'hidden'; 
     makeApiCall(); 
    } 
    else { 
     authorizeButton.style.visibility = ''; 
     authorizeButton.onclick = handleAuthClick; 
    } 
} 

function handleAuthClick(event) { 
    gapi.auth.authorize({client_id: clientId, scope: [scopes], immediate: false}, handleAuthResult); 
    return false; 
} 

function makeApiCall() { 
    gapi.client.load('drive', 'v2', makeRequest); 
} 

function makeRequest() 
{ 
    var request = gapi.client.drive.files.list({'maxResults': 5 }); 

    request.execute(function(resp) {   
     for (i=0; i<resp.items.length; i++) { 
      var titulo = resp.items[i].title; 
      var fechaUpd = resp.items[i].modifiedDate; 
      var userUpd = resp.items[i].lastModifyingUserName; 
      var userEmbed = resp.items[i].embedLink; 
      var userAltLink = resp.items[i].alternateLink; 

      var fileInfo = document.createElement('li'); 
      fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd));     
      document.getElementById('content').appendChild(fileInfo); 
     } 
    });  
} 

我有這樣的錯誤:

Uncaught TypeError: Cannot read property 'files' of undefined 

在該行

var request = gapi.client.drive.files.list({'maxResults': 5 }); 

回答

23

使用

var request = gapi.client.request({ 
     'path': '/drive/v2/files', 
     'method': 'GET', 
     'params': {'maxResults': '1'} 
     }); 

,而不是

var request = gapi.client.drive.files.list({'maxResults': 5 }); 

解決了這個問題!

+1

感謝分享!這適用於我。我發現奇怪的是,在API文檔中作爲示例發佈的代碼無法按預期工作...... – Nielsm

7

代碼看起來沒問題,並且您正確等待,直到gapi.client.load完成。可能只是加載Drive JS文件或其他問題(可能是錯誤的JS文件緩存?)的錯誤。我修改你的例子有點對的jsfiddle運行,看看http://jsfiddle.net/Rbg44/4/爲完整的例子:

HTML:

<button id="authorize-button">Authorize</button> 
<div id="content">Files:</div> 

JS:

var CLIENT_ID = '...'; 
var API_KEY = '...'; 
var SCOPES = '...'; 

function handleClientLoad() { 
    gapi.client.setApiKey(API_KEY); 
    window.setTimeout(checkAuth,1); 
} 

function checkAuth() { 
    var options = { 
     client_id: CLIENT_ID, 
     scope: SCOPES, 
     immediate: true 
    }; 
    gapi.auth.authorize(options, handleAuthResult); 
} 

function handleAuthResult(authResult) { 
    var authorizeButton = document.getElementById('authorize-button'); 

    if (authResult && !authResult.error) { 
     authorizeButton.style.visibility = 'hidden'; 
     makeApiCall(); 
    } else { 
     authorizeButton.style.visibility = ''; 
     authorizeButton.onclick = handleAuthClick; 
    } 
} 

function handleAuthClick(event) { 
    var options = { 
     client_id: CLIENT_ID, 
     scope: SCOPES, 
     immediate: false 
    }; 
    gapi.auth.authorize(options, handleAuthResult); 
    return false; 
} 

function makeApiCall() { 
    gapi.client.load('drive', 'v2', makeRequest); 
} 

function makeRequest() { 
    var request = gapi.client.drive.files.list({'maxResults': 5 }); 
    request.execute(function(resp) {   
     for (i=0; i<resp.items.length; i++) { 
      var titulo = resp.items[i].title; 
      var fechaUpd = resp.items[i].modifiedDate; 
      var userUpd = resp.items[i].lastModifyingUserName; 
      var userEmbed = resp.items[i].embedLink; 
      var userAltLink = resp.items[i].alternateLink; 

      var fileInfo = document.createElement('li'); 
      fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + 
       ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd));     
      document.getElementById('content').appendChild(fileInfo); 
     } 
    });  
} 

$(document).ready(function() { 
    $('#authorize-button').on('click', handleAuthClick); 
    $.getScript('//apis.google.com/js/api.js', function() { 
    gapi.load('auth:client', handleClientLoad); 
    }); 
}); 

你能在你的瀏覽器會檢查如果在調用gapi.client.load()時發生的請求中存在任何問題,請使用開發工具?

4

你需要這樣寫:

gapi.client.load('drive', 'v2', null); 
+1

這是一個有用的回覆。它使谷歌的示例代碼工作。 –