[英]How to create a folder on Google Drive using javascript
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.现在我正在使用此代码将文件上传到 Google Drive: https : //stackoverflow.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:所以谷歌说“内容类型:应用程序/json”进入标题,“应用程序/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".当我将“headers”部分中的“Content-Type”更改为“application/vnd.google-apps.folder”并删除“body”部分时,它正在创建一个名为“Untitled”的文件夹。
How can I get it to create a folder with a specific title?如何让它创建具有特定标题的文件夹?
Finally got it working by googling Claudios code which led me to this piece of code: https://stackoverflow.com/a/11361392/1715263最后通过谷歌搜索 Claudios 代码让它工作,这让我找到了这段代码: 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/".改变的重要事情是“路径”,它现在是“/drive/v2/files/”而不是“/upload/drive/v2/files/”。 I just removed the 'gapi.client.load'-function, added headers information and changed the bodys mimeType.我刚刚删除了'gapi.client.load'-function,添加了标题信息并更改了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;
});
}
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);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.