google drive javascript api gapi.client.drive.files undefined
Asked Answered
G

2

10

The JavaScript example to list files of Google Drive uses gapi.client.drive.files.list(). When trying to use this method I get the error "Cannot read property 'files' of undefined".

The issue and a workaround are already described under Google Drive API javascript

Is the documentation under https://developers.google.com/drive/v2/reference/files/list#try-it incorrect? Or is there a way to use the API as described.

Gaye answered 10/1, 2013 at 16:41 Comment(3)
Duplicate of #11316462Formality
@SteveBazyl: This is not a duplicate; this asks if there is a circumstance for which the example in the documentation is correct (the alternative being that the example is incorrect). The other question asks how to fix the error.Muskellunge
This will fix it: #15590294Odontalgia
B
19

The JavaScript example is correct, but you have to make sure that you only use gapi.client.drive.files (and the other Drive-specific resources) when the Drive library is loaded, i.e. after:

gapi.client.load('drive', 'v2', callback);
Blasting answered 10/1, 2013 at 19:14 Comment(0)
P
8

If it works fine after writing gapi.client.load('drive', 'v2', callback), then all good. In my case it does not work so I wrote below code.

gapi.load('client', function () {
                gapi.client.load('drive', 'v2', function () {
                    var file = gapi.client.drive.files.get({ 'fileId': fileId });
                    file.execute(function (resp) {
                        //Write you code with resp variable
                    });

                });
            });
Packton answered 26/9, 2018 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.