Powershell 更新 Google 表格

问题描述 投票:0回答:2

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

  1. 我在 https://console.cloud.google.com/
  2. 创建一个项目
  3. 启用了 Google Sheets 和 Drive API。
  4. 创建服务帐号并生成私钥(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 = '[email protected]'
$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
2个回答
0
投票

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

我们的解决方案来自此处的指南:将域范围权限委派给服务帐户

我们的想法是让您的服务帐号承担 Google Workspace 中具有您所需权限的帐号角色,在本例中是编辑特定表格。该指南中的说明非常简单;只需在管理控制台中进行快速设置即可。

值得注意的是,我们没有使用

Get-GOAuthTokenService
来获取访问令牌。我们正在使用 Svyatoslav Pidgorny 的 JWT 模块在这里构建 JWT,并使用它通过 https://oauth2.googleapis.com/token 端点获取访问令牌。

我只快速浏览了一下 UMN-Google 的实现,但我发现他们的 JWT 创建在 JWT 有效负载中没有提供任何

sub
参数。通过此参数,您可以指定您希望服务帐户模拟哪个工作区用户。


0
投票

有没有其他方法可以使用 powershell 更新 Google 工作表而不涉及创建项目?...参见...我在一家允许我们使用个人谷歌云(工作表、文档等)的公司工作。 ..但它不允许我们创建具有额外驱动器、工作表和 IAM 访问权限的项目...

你知道还有其他方法吗?...谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.