如何使用 Drive API v3 将文件添加到我的云端硬盘?
How to add file to My Drive using Drive API v3?
Google 驱动器 API v3 最近更改,文件只能存在于一个文件夹中。这破坏了我用来将文件夹加载到每个域用户的“我的云端硬盘”(文件夹 ID 'root')的 Apps 脚本工具。
将文件夹添加到“我的云端硬盘”的新方法是什么?
var service = getService(userEmail);
var url = 'https://www.googleapis.com/drive/v3/files/' + driveId +'?addParents=' + parentId;
Logger.log(url);
var options = {
'contentType': 'application/json',
'method' : 'patch',
'headers' : { Authorization: 'Bearer ' + service.getAccessToken() },
//https://github.com/googleworkspace/apps-script-oauth2
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, options);
var found = response.getResponseCode()
if (found == 200) { found = 'Added to My Drive' } else {
Logger.log('Failed with response code: %s', found);
}
使用Google驱动器的新快捷方式 MIME 类型!您可以参考 Google 的示例 Create a shortcut to a Drive file 以获得提示。请记住,高级驱动器服务仅限于 Drive API v2,但 URLFetchApp 也可以使用 Drive API v3.
以下代码适用于 URLFetchApp。如果您更愿意使用高级驱动服务,请改为查看此答案:
/*
* Create a new shortcut using UrlFetchApp.
*
* @param {String} driveId Id of the file/folder to use as a shortcut target.
* @param {String} userEmail User email address for change context and OAuth token.
* @param {String} parentId The parent folder for the shortcut file.
* @param {String} driveName The display name of the shortcut.
* https://developers.google.com/drive/api/v3/shortcuts
* https://developers.google.com/drive/api/v3/reference/files/create
* https://github.com/googleworkspace/apps-script-oauth2
*/
function TEST_addShortcut() { addShortcut('DriveKey0123456789ABCDEabcde',
'Username@CompanyDomain.com',
'root',
'The file or folder name')
};
function addShortcut(driveId, userEmail, parentId, driveName) {
var service = getService(userEmail); //apps-script-oauth2 library
if (service.hasAccess()) {
Logger.log("addShortcut hasAccess, User %s",userEmail);
var url = 'https://www.googleapis.com/drive/v3/files/';
var data = {
'shortcutDetails': {'targetId': driveId},
'name' : driveName,
'mimeType' : 'application/vnd.google-apps.shortcut',
'parents' : parentId //Use Id 'root' for user's My Drive folder
};
var options = {
'method' : 'POST',
'contentType': 'application/json',
'headers' : { Authorization: 'Bearer ' + service.getAccessToken() },
'payload' : JSON.stringify(data) // Convert JavaScript object to JSON string
};
var response = UrlFetchApp.fetch(url, options);
var resultStringy = JSON.stringify(response, null, 2);
var respCode = response.getResponseCode()
Logger.log('addShortcut code %s, result: %s', respCode, resultStringy);
return (respCode == 200);
} else {
Logger.log('addShortcut getLastError: %s', service.getLastError());
Logger.log('addShortcut(%s) = %s',userEmail,'No Access');
return null; //Service does not have access
}
};
Google 驱动器 API v3 最近更改,文件只能存在于一个文件夹中。这破坏了我用来将文件夹加载到每个域用户的“我的云端硬盘”(文件夹 ID 'root')的 Apps 脚本工具。
将文件夹添加到“我的云端硬盘”的新方法是什么?
var service = getService(userEmail);
var url = 'https://www.googleapis.com/drive/v3/files/' + driveId +'?addParents=' + parentId;
Logger.log(url);
var options = {
'contentType': 'application/json',
'method' : 'patch',
'headers' : { Authorization: 'Bearer ' + service.getAccessToken() },
//https://github.com/googleworkspace/apps-script-oauth2
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, options);
var found = response.getResponseCode()
if (found == 200) { found = 'Added to My Drive' } else {
Logger.log('Failed with response code: %s', found);
}
使用Google驱动器的新快捷方式 MIME 类型!您可以参考 Google 的示例 Create a shortcut to a Drive file 以获得提示。请记住,高级驱动器服务仅限于 Drive API v2,但 URLFetchApp 也可以使用 Drive API v3.
以下代码适用于 URLFetchApp。如果您更愿意使用高级驱动服务,请改为查看此答案:
/*
* Create a new shortcut using UrlFetchApp.
*
* @param {String} driveId Id of the file/folder to use as a shortcut target.
* @param {String} userEmail User email address for change context and OAuth token.
* @param {String} parentId The parent folder for the shortcut file.
* @param {String} driveName The display name of the shortcut.
* https://developers.google.com/drive/api/v3/shortcuts
* https://developers.google.com/drive/api/v3/reference/files/create
* https://github.com/googleworkspace/apps-script-oauth2
*/
function TEST_addShortcut() { addShortcut('DriveKey0123456789ABCDEabcde',
'Username@CompanyDomain.com',
'root',
'The file or folder name')
};
function addShortcut(driveId, userEmail, parentId, driveName) {
var service = getService(userEmail); //apps-script-oauth2 library
if (service.hasAccess()) {
Logger.log("addShortcut hasAccess, User %s",userEmail);
var url = 'https://www.googleapis.com/drive/v3/files/';
var data = {
'shortcutDetails': {'targetId': driveId},
'name' : driveName,
'mimeType' : 'application/vnd.google-apps.shortcut',
'parents' : parentId //Use Id 'root' for user's My Drive folder
};
var options = {
'method' : 'POST',
'contentType': 'application/json',
'headers' : { Authorization: 'Bearer ' + service.getAccessToken() },
'payload' : JSON.stringify(data) // Convert JavaScript object to JSON string
};
var response = UrlFetchApp.fetch(url, options);
var resultStringy = JSON.stringify(response, null, 2);
var respCode = response.getResponseCode()
Logger.log('addShortcut code %s, result: %s', respCode, resultStringy);
return (respCode == 200);
} else {
Logger.log('addShortcut getLastError: %s', service.getLastError());
Logger.log('addShortcut(%s) = %s',userEmail,'No Access');
return null; //Service does not have access
}
};