I have few accounts where I need to transfer the ownership to another account. I have the list listed in google sheets with all the email addresses. I am using the code below to hopefully execute it but I am not sure what to use to execute the code.
function dataTransfer() {
var ss = SpreadsheetApp.openById("SHEETID")
var sheet = ss.getSheetByName("SHEETNAME");
var start = 2;
var end = sheet.getLastRow();
var range = sheet.getRange(start,1,end,sheet.getLastColumn());
var values = range.getValues();
for (var i = 0; i < values.length-1; i++) {
/* transfer the data ownership to */
var A = "[email protected]";
var B = "[email protected]";
var C = "[email protected]";
var email = values[i][0];
var company = values[i][1];
var status = values[i][3];
if (status === "Complete") {continue;}
if (company == "A") {
/* transfer of the ownership of user data between users. */
var transferObject = {
"kind": "admin#datatransfer#DataTransfer",
"oldOwnerUserId": email,
"newOwnerUserId": A,
"applicationDataTransfers": [
{
"applicationId": XXXXXX,
"applicationTransferParams": [{
"key": "PRIVACY_LEVEL",
"value": [
"PRIVATE",
"SHARED"
]
}],
"applicationId": XXXXX,
"applicationTransferParams": [{
"key": "RELEASE_RESOURCES",
"value": true
}],
}],
}
**admin.datatransfer....** /* what do I use here to execute the above code */
sheet.getRange(start+i,4,1,1).setValue("Complete");
}
}
}
}
Answer:
Using
UrlFetchAppyou need to make aPOSTrequest tohttps://admin.googleapis.com/admin/datatransfer/v1/transfers. You have to set up a GCP Project and enable the Data Transfer API to do this.More Information:
The prerequisites for using the Data Transfer API, as per this page state (emphasis my own):
You can do this by going to console.cloud.google.com and creating a new project from the drop-down menu next to
Google Cloud Platformon the upper menu bar.Then, using the hamburger menu follow
≡ > APIs & Services > Libraryand search forAdmin SDK API. Click on the matching result, and on the new page click theENABLEbutton.Next, you will need to link this GCP Project to your Apps Script Project.
Going back to the Hamburger menu, follow
≡ > Home > Dashboardand copy the 12-digit project number displayed in under theProject infoarea.Going back to your Script, follow the
Resources > Cloud Platform project...menu item, paste in that 12-digit number and clickSet Project. This will link your Apps Script Project to the newly created GCP project wit the enabled API.You can safely close this modal now.
As per the documentation on the
transfers.insertmethod:Code:
Firstly, as a quick code fix, the
applicationDataTransfers/applicationTransferParams/valueneeds to be an array of strings, and so should be:As well as this, the
oldOwnerUserIdandnewOwnerUserIdvalues must be the user ID values obtained from theusers.listendpoint. The Google email address does not work as a replacement for this. A full example request would be as follows:As for making the
HTTPrequest:Important Note: As you are using the API directly with the Script's token as your OAuth Token, you will need to update the Apps Script manifest with your required scopes.
Follow the
View > Show manifest filemenu item, and add your scopes to the project. The full manifest file should look something like this:The
script.external_requestis required so you can callUrlFetchApp, and theadmin.datatransferis the scope required to make the call to the directory insert endpoint.References: