使用Google Apps脚本获取其他用户的云端硬盘文件

时间:2014-05-21 12:52:23

标签: google-apps-script google-drive-api ownership

Intro

我的任务是为公司创建共享的Google云端硬盘文件夹结构。 我希望它(或部分)中的所有内容都由同一个用户拥有。 要做到这一点,我想使用Google Apps脚本来强制文件夹中所有文件/文件夹的所有权,以获取文件夹列表。

Problem 

我无法让所有权转让发挥作用。

Thoughts

Google Apps管理员可以强制用户X对用户Y的所有权。但是,由于我只希望共享文件夹中的文件位置转让所有权,因此不会这样做。

以前可以通过Apps脚本完成,第三方功能称为transferOwnership

current implementation of Apps Script我得到"访问被拒绝"。 如果setOwner函数只能由文件所有者使用,那对我来说没用,但如果错误来自其他东西,我想知道是什么。

我是完全管理员,我宁愿使用"真正的"功能库然后是第三方。

2 个答案:

答案 0 :(得分:2)

您可以使用用户凭据运行webapp并触发sinergy。 我将为您提供我在公司实施的解决方案。 每次触发器运行时,下面的系统都会传输属性,我选择每天早上1点运行一次。当然,您可以更改实现,以便可以在您喜欢的任何情况下进行触发,例如从另一个脚本调用它。

让我们假设您有一个特定用户(即域超级管理员)负责保存所有公司文件夹和文件的属性,最后让我们说公司根文件夹是COMPANY

===============================================

首先创建帮助 GAS代理检查文件e文件夹是否属于公司数据,如下所示(必须发布为:作为super admninstrator运行,运行的权限)域中的任何用户):



function doGet(e){
  var id=e.parameter.id;
  var type=e.parameter.type;
  var result="";
  
  if (type=="file"){
    result=isFileInCompanyDataTree(DriveApp.getFileById(id)).toString();
  }
  else if (type=="folder"){
    result=isFolderInCompanyDataTree(DriveApp.getFolderById(id)).toString();
  }
  else
    result="unknow type";
    
  return ContentService.createTextOutput(result);
}


/**
* RECURSIVELY Check if a given folder is in the Company Data Tree
*
* @param {folder} folder for which to check if it is in Company Data Tree
* @return {boolean} true if folder is in Company Data Tree, false otherwise,
*/
function isFolderInCompanyDataTree(folder){
  var isInCompanyData=false;
  var folderId=folder.getId();
  
  if (folderId=="PUT_THE_COMPANY_FOLDER_ID_HERE") return true;
  
  var parents=folder.getParents();
  while (parents.hasNext()) if (isInCompanyData=isFolderInCompanyDataTree(parents.next())) break;
  return isInCompanyData;
}

/**
* RECURSIVELY Check if a given file is in the Company Data Tree
*
* @param {file} file for which to check if is in Company Data Tree
* @return {boolean} true if file is in Company Data Tree, false otherwise,
*/
function isFileInCompanyDataTree(file){
  var isInCompanyData=false;
  var fileParents=file.getParents();
  
  while (fileParents.hasNext()) if (isInCompanyData=isFolderInCompanyDataTree(fileParents.next())) break;
  return isInCompanyData;
}




============================================== < / p>

第二次像这样创建webapp(必须发布为:以用户登录的方式运行,为域中的任何用户运行的权限)

&#13;
&#13;
function doGet(){
  var result="";
  var triggerId;
  
  // Transfer property
  give2AdminstratorFileAndFolderProperty();
  
  var flagAct=PropertiesService.getUserProperties().getProperty("trasfPropActive");

  if (flagAct=="true"){
    // system active, disable trigger
    triggerId=PropertiesService.getUserProperties().getProperty("triggerId");
    if (triggerId){
      var allTriggers = ScriptApp.getProjectTriggers();
      for (var i = 0; i < allTriggers.length; i++) {
        if (allTriggers[i].getUniqueId() == triggerId) {
          ScriptApp.deleteTrigger(allTriggers[i]);
          break;
        }
      }
    }
    PropertiesService.getUserProperties().deleteProperty("trasfPropActive");
    PropertiesService.getUserProperties().deleteProperty("triggerId");
    GmailApp.sendEmail("yourusername@yourdomain.com", "Automatic property transfer DISABLED for "+Session.getEffectiveUser().getEmail(),"DISABLED ");
    result=ContentService.createTextOutput("Thank you. Automatic property transfer DISABLED");  
  }
  // system NOT ACTIVE, enable trigger
  else{
    triggerId= ScriptApp.newTrigger("give2AdminstratorFileAndFolderProperty").timeBased().everyDays(1).atHour(1).create().getUniqueId();
    PropertiesService.getUserProperties().setProperty("trasfPropActive",true);
    PropertiesService.getUserProperties().setProperty("triggerId",triggerId);
    GmailApp.sendEmail("yourusername@yourdomain.com", " Automatic property transfer ENABLED for "+Session.getEffectiveUser().getEmail(),"ENABLED");
    result=ContentService.createTextOutput("THANK YOU. Automatic property transfer ENABLED");
  }
  
  return result;
}

/**
* For each folder and file is in the Company Data Tree owned by 'me', transfer property to the domain super administrator 
*
* @return {folderList} contains the list of folders for which the property changed
*/
function give2AdminstratorFileAndFolderProperty(){
  if (Session.getEffectiveUser().getEmail()=="yourusername@yourdomain.com") return "Nothing done, your are the super administrator! The goal of this script make sense only for user that are NOT the super administrator.";
  var folderChanged="";
  var fileChanged="";
  folderChanged=transferFolderProperty();
  fileChanged=transferFileProperty();
  return "List of folder: "+folderChanged+".\n\nList of files: "+fileChanged;
}

function check_give2AdminstratorFileAndFolderProperty(){
  var folderChanged="";
  var fileChanged="";
  folderChanged=check_transferFolderProperty();
  fileChanged=check_transferFileProperty();
  Logger.log("\n\nList of folder: "+folderChanged+".\n\nList of files: "+fileChanged);
}
/**
* For each folder is in the Company Data Tree owned by 'me', transfer property to the domain super administrator 
*
* @return {folderList} contains the list of folders for which the property changed
*/
function transferFolderProperty(){
  var folders = DriveApp.searchFolders('"me" in owners');
  var folderList="";
  
  
  while (folders.hasNext()) {
    var folder = folders.next();
    if (isFolderInCompanyDataTree(folder)){
      folder.setOwner("yourusername@yourdomain.com");
      folderList=folder.getName()+", ";
    }
  }
  return folderList.slice(0,-2);
}

function check_transferFolderProperty(){
  var folders = DriveApp.searchFolders('"me" in owners');
  var folderList="";
  
  
  while (folders.hasNext()) {
    var folder = folders.next();
    if (isFolderInCompanyDataTree(folder)){
      folderList=folder.getName()+", ";
    }
  }
  return folderList.slice(0,-2);
}

/**
* For each file is in the Company Data Tree that is owned by 'me', transfer property to the domain super administrator 
*
* @return {fileList} contains the list of files for which the property changed
*/
function transferFileProperty(){
  var files = DriveApp.searchFiles('"me" in owners');
  var fileList=""; 
  while (files.hasNext()) {
    var file = files.next();
    if (isFileInCompanyDataTree(file)){
      file.setOwner("yourusername@yourdomain.com");
      fileList=file.getName()+", ";
    }
  }
  return fileList.slice(0,-2);
}

function check_transferFileProperty(){
  var files = DriveApp.searchFiles('"me" in owners');
  var fileList=""; 
  while (files.hasNext()) {
    var file = files.next();
    if (isFileInCompanyDataTree(file)){
      fileList=file.getName()+", ";
    }
  }
  return fileList.slice(0,-2);
}


/**
* Check if a given folder is in the Company Data Tree
*
* @param {folder} folder for which to check if is in Company Data Tree
* @return {boolean} true if folder is in Company Data Tree, false otherwise,
*/
function isFolderInCompanyDataTree(folder){
  var token = ScriptApp.getOAuthToken();
  
  var response=UrlFetchApp.fetch("PUT_HERE_THE_URL_OF_THE_PROXY_CREATED_AT_STEP_ONE"+"?id="+folder.getId()+"&type=folder", {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  }).getContentText();
  return (response==="true");
}


/**
* Check if a given file is in the Company Data Tree
*
* @param {file} file for which to check if is in Company Data Tree
* @return {boolean} true if file is in Company Data Tree, false otherwise,
*/
function isFileInCompanyDataTree(file){
  var token = ScriptApp.getOAuthToken();
  var response=UrlFetchApp.fetch("PUT_HERE_THE_URL_OF_THE_PROXY_AT_CREATED_AT_STEP_ONE"+"?id="+file.getId()+"&type=file", {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  }).getContentText();
  return (response==="true");
}
&#13;
&#13;
&#13;

第二个脚本的网址分发给域用户,要求他们允许其提出的权限。 ...或者根据您的喜好玩实施... 享受

答案 1 :(得分:1)

见&amp; star Issue 2756:非法ACL更改的服务器错误

摘要:只有文件所有者才能更改所有权。管理员帐户不拥有用户的文件,因此他们在这方面没有特殊权限。

正如@ HenriqueAbreu的评论中所述,this answer中描述了使用Drive API(而不是Google Apps脚本)模拟域用户的可行方法,遗憾的是没有实现细节。