如何使用javascript在Google云端硬盘上创建文件夹

时间:2022-08-10 01:20:02

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

现在我正在使用此代码将文件上传到Google云端硬盘:https://*.com/a/11657773/1715263它可以正常使用文本文件。

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

使用相同的代码,我正在尝试使用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:

谷歌说“Content-Type:application / json”进入标题,“application / vnd.google-apps.folder”应该是正文中的mimetype(?),这就是我在我的代码中所做的事情,现在看起来像这样:

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.

但它只创建一个名为“Untitled”的文件,它不是文件夹,你无法打开它。

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".

当我将“标题”部分中的“内容类型”更改为“application / vnd.google-apps.folder”并删除“正文”部分时,它会创建一个名为“无标题”的文件夹。

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

如何创建具有特定标题的文件夹?

2 个解决方案

#1


5  

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

最后通过谷歌搜索Claudios代码使我得到这段代码:https://*.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.

改变的重要一点是'path',它现在是“/ drive / v2 / files /”而不是“/ upload / drive / v2 / files /”。我刚删除'gapi.client.load'函数,添加了头信息并更改了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;
   });
}

#2


3  

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

#1


5  

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

最后通过谷歌搜索Claudios代码使我得到这段代码:https://*.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.

改变的重要一点是'path',它现在是“/ drive / v2 / files /”而不是“/ upload / drive / v2 / files /”。我刚删除'gapi.client.load'函数,添加了头信息并更改了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;
   });
}

#2


3  

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