【问题标题】:Powershell to Update Google SheetsPowershell 更新 Google 表格
【发布时间】:2022-01-25 00:29:02
【问题描述】:

我在 PowerShell 中从 https://github.com/umn-microsoft-automation/UMN-Google 导入了一个模块 UMN-Google,用于创建、更新 Google 表格。

  1. 我在https://console.cloud.google.com/ 中创建了一个项目
  2. 启用了 Google 表格和云端硬盘 API。
  3. 创建服务帐号并生成私钥(P12 格式)。

以下是生成访问令牌的 PowerShell 代码,相同的访问令牌用于创建或更新工作正常的工作表。

Import-Module UMN-Google

# Set security protocol to TLS 1.2 to avoid TLS errors
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Google API Authozation
$scope = "https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file"
$certPath = "C:\Temp\sheets-script-336006-5db4e6b09111.p12"
$iss = 'sheets-script@sheets-script-336006.iam.gserviceaccount.com'
$certPswd = 'notasecret'
try {
    $accessToken = Get-GOAuthTokenService -scope $scope -certPath $certPath -certPswd $certPswd -iss $iss
} catch {
    $err = $_.Exception
    $err | Select-Object -Property *
    "Response: "
    $err.Response
}

$accessToken

# Create new spreadsheet
$Title = 'Patching Spreadsheet'
$SpreadsheetID = (New-GSheetSpreadSheet -accessToken $accessToken -title $Title).spreadsheetId
$SpreadsheetID

# Create new sheet
$Sheet = 'Computers'
Add-GSheetSheet -accessToken $accessToken -sheetName $Sheet -spreadSheetID $SpreadsheetID

现在我有许多谷歌表格,我希望将脚本集成到这些表格中,以便更新各种表格中的数据。我们已禁用域外的驱动器共享,因此我无法授予服务帐户编辑工作表的权限。

是否有任何方法或可能性可以使服务帐户有权更新工作表。

【问题讨论】:

    标签: powershell google-sheets google-cloud-platform


    【解决方案1】:

    这与我最近遇到的一些事情有关,特别是让服务帐户在我们的 Google Workspace 中“模拟”用户以执行对 Google Drive API 的调用。

    我们的解决方案来自本指南:Delegating domain-wide authority to the service account

    我们的想法是让您的服务帐户在您的 Google Workspace 中担任一个帐户的角色,该帐户确实具有您需要的权限,在这种情况下可以编辑特定的表格。该指南中的说明非常简单;只需在管理控制台中进行快速设置。

    值得注意的是,我们没有使用Get-GOAuthTokenService 来获取访问令牌。我们正在使用Svyatoslav Pidgorny's JWT module here 构建一个JWT,并使用它通过the https://oauth2.googleapis.com/token endpoint 获取访问令牌。

    我只快速浏览了at UMN-Google's implementation,但我发现他们的 JWT 创建没有在 JWT 有效负载中提供任何 sub 参数。通过该参数,您可以指定希望服务帐户模拟的 Workspace 用户。

    【讨论】:

      猜你喜欢
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      相关资源
      最近更新 更多