【问题标题】:Google Drive API javascript谷歌驱动 API javascript
【发布时间】:2012-07-04 04:23:34
【问题描述】:

我正在尝试使用 Google 驱动器列出文件。

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

【问题讨论】:

    标签: javascript google-drive-api


    【解决方案1】:

    使用

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

    而不是

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

    解决了问题!

    【讨论】:

    • 感谢分享!这对我有用。我确实觉得在 API 文档中作为示例发布的代码没有按预期工作很奇怪......
    【解决方案2】:

    代码看起来不错,您正在等待 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() 时发出的请求有任何问题,您能否检查您的浏览器开发工具?

    【讨论】:

      【解决方案3】:

      你需要这样写:

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

      【讨论】:

      • 这是一个有用的回复。它使来自 Google 的示例代码工作。
      猜你喜欢
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多