本文介绍了如何执行数据传输API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将所有权转移到另一个帐户的帐户很少.我在Google表格中列出了所有电子邮件地址的列表.我正在使用下面的代码来希望执行它,但是我不确定该使用什么来执行代码.

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.

感谢您的宝贵时间和帮助!TIA!

I appreciate your time and help! TIA!

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");
        }
    }
  }
}

推荐答案

答案:

使用 UrlFetchApp ,您需要对 https://admin.googleapis.com/admin/datatransfer/v1/transfers发出 POST 请求.您必须设置一个GCP项目并启用数据传输API来执行此操作.

根据使用Data Transfer API的前提条件此页面状态(强调我自己的状态):

The prerequisites for using the Data Transfer API, as per this page state (emphasis my own):

  • 拥有一个Google帐户并创建一个管理员.该API适用于Google Workspace,教育,政府,代理商和ISP帐户.
  • 熟悉admin.google.com上的Google Workspace管理控制台.有关管理控制台的更多信息,请参阅使用管理控制台.
  • 从Google Workspace管理控制台启用API访问权限,以便向数据传输API发出请求.

您可以通过以下方法执行此操作:转到 console.cloud.google.com 并创建一个新项目从上方菜单栏上 Google Cloud Platform 旁边的下拉菜单中.

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 Platform on the upper menu bar.

然后,使用汉堡包菜单,按照≡>API和服务>库,然后搜索 Admin SDK API .单击匹配的结果,然后在新页面上单击 ENABLE 按钮.

Then, using the hamburger menu follow ≡ > APIs & Services > Library and search for Admin SDK API. Click on the matching result, and on the new page click the ENABLE button.

下一步,您需要将此GCP项目链接到您的Apps脚本项目.

Next, you will need to link this GCP Project to your Apps Script Project.

返回汉堡包"菜单,按照≡>主页>仪表板,然后复制显示在 Project info 区域下方的12位项目编号.

Going back to the Hamburger menu, follow ≡ > Home > Dashboard and copy the 12-digit project number displayed in under the Project info area.

返回脚本,按照 Resources>Cloud Platform项目... 菜单项,粘贴该12位数字,然后单击设置项目.这会将您的Apps脚本项目链接到启用了API的新创建的GCP项目.

Going back to your Script, follow the Resources > Cloud Platform project... menu item, paste in that 12-digit number and click Set 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.

根据 transfers.insert 方法:

POST https://admin.googleapis.com/admin/datatransfer/v1/transfers

请求正文包含 DataTransfer .

The request body contains an instance of DataTransfer.

代码:

首先,作为快速代码修复, applicationDataTransfers/applicationTransferParams/value 必须为字符串数组,因此应为:

"applicationId": XXXXX,
  "applicationTransferParams": [
    {
      "key": "RELEASE_RESOURCES",
      "value": [
        "TRUE"
      ]
    }
  ],
// ...

此外, oldOwnerUserId newOwnerUserId 值必须是从 users.list 端点.Google电子邮件地址 无效 替代了此地址.完整的示例请求如下:

As well as this, the oldOwnerUserId and newOwnerUserId values must be the user ID values obtained from the users.list endpoint. The Google email address does not work as a replacement for this. A full example request would be as follows:

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脚本清单.

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.

按照 View>显示清单文件菜单项,然后将范围添加到项目中.完整的清单文件应如下所示:

Follow the View > Show manifest file menu item, and add your scopes to the project. The full manifest file should look something like this:

{
  "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 是进行调用的必要范围目录插入端点.

The script.external_request is required so you can call UrlFetchApp, and the admin.datatransfer is the scope required to make the call to the directory insert endpoint.

希望对您有帮助!

  • 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

这篇关于如何执行数据传输API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 00:14
查看更多