Apuleius
Apuleius

Reputation: 450

google drive api v3 create folder fails silently

After successfully authenticating via OAuth with full scope:

https://www.googleapis.com/auth/drive` 

I create a folder according to the example in Creating a folder

   var fileMetadata = {
        'name' : name,
        'mimeType' : 'application/vnd.google-apps.folder',
//        'parents': [ parent ]
    };
   gapi.client.drive.files.create({
       resource: fileMetadata,
       fields: 'id'
    }, function(err, file) {
      if(err) {
        // Handle error
        console.log(err);
      } else {
        console.log('Folder Id: ', file.id);
        return file.id;
      }
    });

The callback function is never invoked, no error in the console.

How can i find out what's going on?

Upvotes: 3

Views: 1185

Answers (2)

W.S.
W.S.

Reputation: 1505

The function gapi.client.drive.files.create({...}) returns a promise, so don't pass a callback function, but instead use a .then() block to execute the promise.

var fileMetadata = {
    'name': FILE_NAME,
    'mimeType': FILE_MIME_TYPE,
    // 'parents': [ parentFolderID ]
};

gapi.client.drive.files.create({
    resource: fileMetadata,
    fields: '*'
}).then(function(response) {
    if (response.status === 200) {
        var file = response.result;
        console.log(file);
    }
}).catch(function(error) {
    console.error(error);
);

Upvotes: 0

Apuleius
Apuleius

Reputation: 450

I've ended up using the lower level gapi.client.request method, which works reliably.

var body= {"name": name, 
           "mimeType": "application/vnd.google-apps.folder",
           "parents": [parent.id]}

gapi.client.request({
  'path': 'https://www.googleapis.com/drive/v3/files/',
  'method': 'POST',
  'body': body
}).then(function(jsonResp,rawResp) {
    console.log(jsonResp)
    if (jsonResp.status==200) {
      callback(jsonResp.result)
    }
})

Upvotes: 5

Related Questions