4

Right now I'm using this code to upload files to Google Drive: https://stackoverflow.com/a/11657773/1715263 It works fine with a textfile.

With the same code I'm trying to create a folder, using this information from Google: https://developers.google.com/drive/folder

so Google says "Content-Type: application/json" goes into the header and "application/vnd.google-apps.folder" should be the mimetype in the body(?), thats what I'm doing in my code, which looks like this now:

function createFolder() 
{
    var access_token = googleAuth.getAccessToken();

    var json = JSON.stringify({
        mimeType: 'application/vnd.google-apps.folder',
        title: 'Folder',
    });

    var body =  "Content-Type: application/json" + "\r\n" +
                "Content-Length: " + json.length + "\r\n" + "\r\n" +
                json;

    gapi.client.request({

        'path': '/upload/drive/v2/files/',
        'method': 'POST',
        'params': {'uploadType': 'multipart'},
        'headers': {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + access_token,             
        },
        'body': body
    }).execute(function(file) { 
        document.getElementById("info").innerHTML = "Created folder: " + file;
    }); 

But it's only creating a file named "Untitled", it's no folder and you can't open it.

When I change the "Content-Type" in the "headers" section to "application/vnd.google-apps.folder" and remove the "body" part, it's creating a folder named "Untitled".

How can I get it to create a folder with a specific title?

Community
  • 1
  • 1
Jex
  • 131
  • 1
  • 10

2 Answers2

7

Finally got it working by googling Claudios code which led me to this piece of code: https://stackoverflow.com/a/11361392/1715263

The important thing that changed is the 'path', its now "/drive/v2/files/" instead of "/upload/drive/v2/files/". I just removed the 'gapi.client.load'-function, added headers information and changed the bodys mimeType.

So here's the code:

function createFolder() {

   var access_token = googleAuth.getAccessToken();

   var request = gapi.client.request({
       'path': '/drive/v2/files/',
       'method': 'POST',
       'headers': {
           'Content-Type': 'application/json',
           'Authorization': 'Bearer ' + access_token,             
       },
       'body':{
           "title" : "Folder",
           "mimeType" : "application/vnd.google-apps.folder",
       }
   });

   request.execute(function(resp) { 
       console.log(resp); 
       document.getElementById("info").innerHTML = "Created folder: " + resp.title;
   });
}
Community
  • 1
  • 1
Jex
  • 131
  • 1
  • 10
  • Is the only way to check if the file already exists is to perform a search? Or is it also possible to pass a unique ID yourself for example? – dvtoever Apr 10 '13 at 19:56
  • 1
    when you create a folder or upload a file you can access its unique ID in the "request.execute" callback function via "resp.id" (in my code example). you can then save that ID somewhere and access its related file like shown here http://stackoverflow.com/a/14431112/1715263 finally, just add some error handling: `if (!resp.error) {console.log("File already exists: " + resp.title);} else {console.log("File not found: " + resp.error.message);` – Jex Apr 11 '13 at 23:46
  • Thanks a lot. I think your answer will help other users indeed. However, Google Drive *is* my only storage so unfortunately I cannot save the id. I only know the filename itself. So I guess I am stuck with searching. – dvtoever Apr 12 '13 at 06:40
  • I see. The only solution I can think of is giving your file a unique filename by adding some random characters and then searching for it, its not that different from accessing a file by its ID, since its all indexed somehow anyways. Another way would be using the 'Application Data folder', thats a special folder that is only accessible by your application, so you can make sure there is no duplicate of your file: https://developers.google.com/drive/appdata – Jex Apr 12 '13 at 11:24
4

Try the following code:

function createFolder(folderName) {
  var body = {
    'title': folderName,
    'mimeType': "application/vnd.google-apps.folder"
  };

  var request = gapi.client.drive.files.insert({
    'resource': body
  });

  request.execute(function(resp) {
    console.log('Folder ID: ' + resp.id);
  });
}
Claudio Cherubino
  • 14,896
  • 1
  • 35
  • 42
  • Hey! Thank you for the fast reply :) When I tried your code I got error "Uncaught TypeError: Cannot read property 'files' of undefined" Then I googled your code and found this: http://stackoverflow.com/a/11361392/1715263 which helped me to finally get it working :) – Jex Oct 03 '12 at 14:28
  • 'name' instead of 'title', at least for drive v2 – Dmitry Bryliuk Feb 15 '18 at 07:06