How to use a service account to transfer ownership

2019-09-16 14:08发布

问题:

I created a service account using the following: https://developers.google.com/drive/web/delegation I downloaded the file it asked me to download as a JSON file if that makes a difference.

Now how do I use that service account to initiate an ownership transfer using app script?

function transferOwnership() {
  var fileId = "1YzwfuawY8OiFIw-nbUCagTUhrxmqH2PEyMmYz1NMT9A";

  var p = {};
  p.role = "owner";
  p.type = "user";
  p.value = "email@ofnewowner.com";

  Drive.Permissions.insert(p,fileId);
}

回答1:

If the file is in Drive, you should try doing something like this:

 function transferOwnership() {
  var fileId = "1YzwfuawY8OiFIw-nbUCagTUhrxmqH2PEyMmYz1NMT9A";

  var owner = "email@ofnewowner.com";

  Drive.getFileById(fileId).setOwner(owner);
}

Hope this helps!



回答2:

I was able to successfully transfer ownership of a file using a service account, via app script api, by using the library and instructions provided by Spencer Easton.

https://github.com/Spencer-Easton/Apps-Script-Drive-Service-Account-Libarary/blob/master/src/saDrive.gs