2012-07-03 50 views
13

Tôi đang cố gắng sử dụng ổ đĩa Google để liệt kê các tệp.API Google Drive javascript

Sử dụng câu trả lời trong https://stackoverflow.com/a/11280257 Tôi đã tìm thấy sự cố mà tôi không thể khám phá lý do.

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); 
     } 
    });  
} 

Tôi có lỗi này:

Uncaught TypeError: Cannot read property 'files' of undefined 

trong dòng

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

Trả lời

23

Sử dụng

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

thay vì

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

giải quyết được sự cố!

+1

Cảm ơn đã chia sẻ! Điều này làm việc cho tôi. Tôi thấy thật lạ khi mã được đăng dưới dạng ví dụ trong tài liệu API không hoạt động như mong đợi ... – Nielsm

7

Mã có vẻ OK và bạn đang đợi chính xác cho đến khi gapi.client.load hoàn tất. Có thể chỉ là một lỗi khi tải các tệp JS Drive hoặc một số vấn đề khác (có thể là tệp JS xấu được lưu trong bộ nhớ cache?). Tôi sửa đổi ví dụ của bạn một chút để chạy trên jsfiddle, hãy nhìn vào http://jsfiddle.net/Rbg44/4/ cho ví dụ đầy đủ:

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); 
    }); 
}); 

Bạn có thể kiểm tra trong các trình duyệt của bạn công cụ dev nếu có bất kỳ loại vấn đề nào trong yêu cầu được thực hiện khi bạn gọi gapi.client.load()?

4

Bạn cần phải viết này:

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

Đây là một phản hồi hữu ích. Nó làm cho mã ví dụ từ Google hoạt động. –

Các vấn đề liên quan