如何执行数据传输API?
How to execute Data Transfer API?
我有几个帐户需要将所有权转移到另一个帐户。我在 google 表中列出了包含所有电子邮件地址的列表。我正在使用下面的代码来执行它,但我不确定使用什么来执行代码。
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 = "aUser@company1.com";
var B = "bUser@company1.com";
var C = "cUser@company1.com";
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");
}
}
}
}
答案:
使用 UrlFetchApp
您需要向 https://admin.googleapis.com/admin/datatransfer/v1/transfers
发出 POST
请求。您必须设置 GCP 项目并启用数据传输 API 才能执行此操作。
更多信息:
使用数据传输API的先决条件,根据this page状态(强调我自己的):
You need the following basic configuration before you can use the Data Transfer API:
- Have a Google account and create an administrator. The API applies to Google Workspace, Education, Government, Reseller, and ISP accounts.
- Be familiar with your Google Workspace Admin console found at admin.google.com. For more information about the Admin console, see Use your Admin console.
- Enable API access from the Google Workspace Admin console in order to make requests to the Data Transfer API.
您可以转到 console.cloud.google.com 并从上方菜单栏 Google Cloud Platform
旁边的下拉菜单中创建一个新项目。
然后,使用汉堡菜单跟随 ≡ > APIs & Services > Library
并搜索 Admin SDK API
。点击匹配结果,在新页面点击ENABLE
按钮。
接下来,您需要将此 GCP 项目link添加到您的 Apps 脚本项目中。
返回汉堡包菜单,按照≡ > Home > Dashboard
复制Project info
区域下方显示的12位项目编号。
返回您的脚本,按照 Resources > Cloud Platform project...
菜单项,粘贴该 12 位数字并单击 Set Project
。这会将 link 您的 Apps 脚本项目添加到启用 API.
的新创建的 GCP 项目
您现在可以安全地关闭此模式。
根据 transfers.insert
method 上的文档:
Inserts a data transfer request.
HTTP request
POST https://admin.googleapis.com/admin/datatransfer/v1/transfers
Request body
The request body contains an instance of DataTransfer
.
代码:
首先,作为快速代码修复,applicationDataTransfers/applicationTransferParams/value
需要是 array of strings,因此应该是:
"applicationId": XXXXX,
"applicationTransferParams": [
{
"key": "RELEASE_RESOURCES",
"value": [
"TRUE"
]
}
],
// ...
除此之外,oldOwnerUserId
和 newOwnerUserId
值必须是从 users.list
端点获取的用户 ID 值。 Google 电子邮件地址 无法替代 。完整的请求示例如下:
var payload = {
"kind": "admin#datatransfer#DataTransfer",
"oldOwnerUserId": "113412843513541354193",
"newOwnerUserId": "118387348413214353832",
"applicationDataTransfers": [
{
"applicationId": "14186345432",
"applicationTransferParams": [
{
"key": "PRIVACY_LEVEL",
"value": [
"PRIVATE",
"SHARED"
]
}
]
},
{
"applicationId": "546343454353",
"applicationTransferParams": [
{
"key": "RELEASE_RESOURCES",
"value": [
"TRUE"
]
}
]
}
]
}
关于提出 HTTP
请求:
const url = "https://admin.googleapis.com/admin/datatransfer/v1/transfers";
var headers ={
"Authorization": 'Bearer ' + ScriptApp.getOAuthToken(),
};
var options = {
"method": "POST",
"headers": headers,
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
重要说明:当您直接使用 API 并将脚本的令牌作为您的 OAuth 令牌时,您需要使用所需的更新 Apps 脚本清单范围。
按照 View > Show manifest file
菜单项,将您的范围添加到项目中。完整清单文件应如下所示:
{
"timeZone": "Europe/Paris",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/admin.datatransfer"
]
}
script.external_request
是必需的,因此您可以调用 UrlFetchApp
,admin.datatransfer
是调用目录插入端点所需的范围。
参考文献:
- ApplicationTransferParam | Data Transfer API | Google Developers
- Google Cloud Platform
- Prerequisites | Data Transfer API | Google Developers
- Method: Transfers.insert | Data Transfer API | Google Developers
- REST Resource: transfers | Data Transfer API | Google Developers
- Method: users.list | Directory API | Google Developers
- Class UrlFetchApp | Apps Script | Google Developers
- Class ScriptApp | Apps Script | Google Developers
我有几个帐户需要将所有权转移到另一个帐户。我在 google 表中列出了包含所有电子邮件地址的列表。我正在使用下面的代码来执行它,但我不确定使用什么来执行代码。
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 = "aUser@company1.com";
var B = "bUser@company1.com";
var C = "cUser@company1.com";
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");
}
}
}
}
答案:
使用 UrlFetchApp
您需要向 https://admin.googleapis.com/admin/datatransfer/v1/transfers
发出 POST
请求。您必须设置 GCP 项目并启用数据传输 API 才能执行此操作。
更多信息:
使用数据传输API的先决条件,根据this page状态(强调我自己的):
You need the following basic configuration before you can use the Data Transfer API:
- Have a Google account and create an administrator. The API applies to Google Workspace, Education, Government, Reseller, and ISP accounts.
- Be familiar with your Google Workspace Admin console found at admin.google.com. For more information about the Admin console, see Use your Admin console.
- Enable API access from the Google Workspace Admin console in order to make requests to the Data Transfer API.
您可以转到 console.cloud.google.com 并从上方菜单栏 Google Cloud Platform
旁边的下拉菜单中创建一个新项目。
然后,使用汉堡菜单跟随 ≡ > APIs & Services > Library
并搜索 Admin SDK API
。点击匹配结果,在新页面点击ENABLE
按钮。
接下来,您需要将此 GCP 项目link添加到您的 Apps 脚本项目中。
返回汉堡包菜单,按照≡ > Home > Dashboard
复制Project info
区域下方显示的12位项目编号。
返回您的脚本,按照 Resources > Cloud Platform project...
菜单项,粘贴该 12 位数字并单击 Set Project
。这会将 link 您的 Apps 脚本项目添加到启用 API.
您现在可以安全地关闭此模式。
根据 transfers.insert
method 上的文档:
Inserts a data transfer request.
HTTP request
POST https://admin.googleapis.com/admin/datatransfer/v1/transfers
Request body
The request body contains an instance of
DataTransfer
.
代码:
首先,作为快速代码修复,applicationDataTransfers/applicationTransferParams/value
需要是 array of strings,因此应该是:
"applicationId": XXXXX,
"applicationTransferParams": [
{
"key": "RELEASE_RESOURCES",
"value": [
"TRUE"
]
}
],
// ...
除此之外,oldOwnerUserId
和 newOwnerUserId
值必须是从 users.list
端点获取的用户 ID 值。 Google 电子邮件地址 无法替代 。完整的请求示例如下:
var payload = {
"kind": "admin#datatransfer#DataTransfer",
"oldOwnerUserId": "113412843513541354193",
"newOwnerUserId": "118387348413214353832",
"applicationDataTransfers": [
{
"applicationId": "14186345432",
"applicationTransferParams": [
{
"key": "PRIVACY_LEVEL",
"value": [
"PRIVATE",
"SHARED"
]
}
]
},
{
"applicationId": "546343454353",
"applicationTransferParams": [
{
"key": "RELEASE_RESOURCES",
"value": [
"TRUE"
]
}
]
}
]
}
关于提出 HTTP
请求:
const url = "https://admin.googleapis.com/admin/datatransfer/v1/transfers";
var headers ={
"Authorization": 'Bearer ' + ScriptApp.getOAuthToken(),
};
var options = {
"method": "POST",
"headers": headers,
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
重要说明:当您直接使用 API 并将脚本的令牌作为您的 OAuth 令牌时,您需要使用所需的更新 Apps 脚本清单范围。
按照 View > Show manifest file
菜单项,将您的范围添加到项目中。完整清单文件应如下所示:
{
"timeZone": "Europe/Paris",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/admin.datatransfer"
]
}
script.external_request
是必需的,因此您可以调用 UrlFetchApp
,admin.datatransfer
是调用目录插入端点所需的范围。
参考文献:
- ApplicationTransferParam | Data Transfer API | Google Developers
- Google Cloud Platform
- Prerequisites | Data Transfer API | Google Developers
- Method: Transfers.insert | Data Transfer API | Google Developers
- REST Resource: transfers | Data Transfer API | Google Developers
- Method: users.list | Directory API | Google Developers
- Class UrlFetchApp | Apps Script | Google Developers
- Class ScriptApp | Apps Script | Google Developers