我正在尝试使用Terraform从Google表格中提取数据来创建BQ表,这是我的external_data_configuration块
resource "google_bigquery_table" "sheet" {
dataset_id = google_bigquery_dataset.bq-dataset.dataset_id
table_id = "sheet"
external_data_configuration {
autodetect = true
source_format = "GOOGLE_SHEETS"
google_sheets_options {
skip_leading_rows = 1
}
source_uris = [
"https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxx",
]
}
我将文件公开,但是当我尝试创建表时出现错误:
错误:googleapi:错误400:读取表格:工作表时出错,错误消息:无法读取电子表格。错误:找不到具有Google云端硬盘范围的OAuth令牌。无效
我阅读了Terraform文档,似乎我需要在我的provider.tf文件中指定access_token和范围,我只是不知道该怎么做,因为我认为这将与我当前的身份验证方法(服务帐户)冲突
将scopes参数添加到你的provider.tf
provider "google" {
credentials = "${file("${var.path}/secret.json")}"
scopes = ["https://www.googleapis.com/auth/drive","https://www.googleapis.com/auth/bigquery"]
project = "${var.project_id}"
region = "${var.gcp_region}"
}
你需要为Google驱动程序和Bigquery添加范围
我怀疑你只需要提供范围,同时保留现有的服务帐户凭据即可。服务帐户凭据文件未指定范围。根据terraform文档,默认情况下使用以下范围:
> https://www.googleapis.com/auth/compute
> https://www.googleapis.com/auth/cloud-platform
> https://www.googleapis.com/auth/ndev.clouddns.readwrite
> https://www.googleapis.com/auth/devstorage.full_control
> https://www.googleapis.com/auth/userinfo.email
默认情况下,大多数GCP服务都接受并使用云平台范围。但是,Google云端硬盘不接受/使用云平台范围,因此BigQuery中的此特定功能需要指定其他范围。为了完成这项工作,你应该使用Google Drive范围https://www.googleapis.com/auth/drive
(相关的BQ文档)来扩展默认的terraform范围列表。有关已记录范围的更详尽列表,请参阅https://developers.google.com/identity/protocols/oauth2/scopes
访问令牌意味着你已经通过了身份验证流程并提供了必要的作用域,因此,你既要提供作用域又要提供令牌是没有意义的。你可以使用范围生成令牌,或者将服务帐户用于其他范围。
希望这可以帮助。