Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API javascript

I'm trying to use the Google drive to list files.

Using the answer in https://stackoverflow.com/a/11280257 I found a problem that I can't discover the reason.

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

I have this error:

Uncaught TypeError: Cannot read property 'files' of undefined 

in the line

var request = gapi.client.drive.files.list({'maxResults': 5 });
like image 987
Sandro Avatar asked Jul 03 '12 17:07

Sandro


People also ask

Is there an API for Google Drive?

The Google Drive API allows you to create apps that leverage Google Drive cloud storage. You can develop applications that integrate with Drive, and create robust functionality in your application using the Drive API.

Is Google Drive API free?

All use of the Drive API is available at no additional cost.


2 Answers

Using

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

instead of

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

resolved the problem!

like image 116
Sandro Avatar answered Oct 07 '22 23:10

Sandro


Code looks OK and you're correctly waiting until gapi.client.load completes. Might just be an error with loading the Drive JS files or some other issue (maybe bad JS file cached?). I modified your example a little bit to run on jsfiddle, take a look at http://jsfiddle.net/Rbg44/4/ for the full example:

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

Can you check in your browsers dev tools if there is any sort of issue in the request made when you call gapi.client.load()?

like image 25
Steve Bazyl Avatar answered Oct 08 '22 00:10

Steve Bazyl