【发布时间】:2022-01-25 00:29:02
【问题描述】:
我在 PowerShell 中从 https://github.com/umn-microsoft-automation/UMN-Google 导入了一个模块 UMN-Google,用于创建、更新 Google 表格。
- 我在https://console.cloud.google.com/ 中创建了一个项目
- 启用了 Google 表格和云端硬盘 API。
- 创建服务帐号并生成私钥(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