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?

4

2 に答える 2

7

最後に、Claudiosコードをグーグルで検索して、このコードにたどり着きました:https ://stackoverflow.com/a/11361392/1715263

変更された重要なことは「パス」であり、「/ upload / drive / v2 /files/」ではなく「/drive/ v2 /files/」になりました。'gapi.client.load'関数を削除し、ヘッダー情報を追加し、本文のmimeTypeを変更しました。

コードは次のとおりです。

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;
   });
}
于 2012-10-03T14:26:58.257 に答える
4

次のコードを試してください。

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);
  });
}
于 2012-10-02T22:46:30.617 に答える