Created
December 27, 2016 08:54
-
-
Save auycro/650b5d4fa0d9722fc741bfd5554a2e53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //FROM http://stackoverflow.com/questions/10796464/transfer-ownership-of-a-file-to-another-user-in-google-apps-script | |
| /** | |
| * Change Owner of a file or folder | |
| * Run this as admin and authorise first in the script editor. | |
| */ | |
| function changeOwner(newOwnerEmail, fileOrFolderId){ | |
| var file = DocsList.getFileById(fileOrFolderId); | |
| var oldOwnerEmail = file.getOwner().getEmail(); | |
| if (oldOwnerEmail === newOwnerEmail) { | |
| return; | |
| } | |
| file.removeEditor(newOwnerEmail); | |
| var base = 'https://docs.google.com/feeds/'; | |
| var fetchArgs = googleOAuth_('docs', base); | |
| fetchArgs.method = 'POST'; | |
| var rawXml = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>" | |
| +"<category scheme='http://schemas.google.com/g/2005#kind' " | |
| +"term='http://schemas.google.com/acl/2007#accessRule'/>" | |
| +"<gAcl:role value='owner'/>" | |
| +"<gAcl:scope type='user' value='"+newOwnerEmail+"'/>" | |
| +"</entry>"; | |
| fetchArgs.payload = rawXml; | |
| fetchArgs.contentType = 'application/atom+xml'; | |
| var url = base + encodeURIComponent(oldOwnerEmail) + '/private/full/'+fileOrFolderId+'/acl?v=3&alt=json'; | |
| var content = UrlFetchApp.fetch(url, fetchArgs).getContentText(); | |
| } | |
| //Google oAuth | |
| function googleOAuth_(name,scope) { | |
| var oAuthConfig = UrlFetchApp.addOAuthService(name); | |
| oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); | |
| oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); | |
| oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); | |
| oAuthConfig.setConsumerKey("anonymous"); | |
| oAuthConfig.setConsumerSecret("anonymous"); | |
| return {oAuthServiceName:name, oAuthUseToken:"always"}; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment