Warm tip: This article is reproduced from serverfault.com, please click

google drive api-Bigquery使用Terraform从Sheet文件创建表

(google drive api - Bigquery create table from Sheet files using Terraform)

发布于 2020-06-06 15:17:09

我正在尝试使用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添加范围

Questioner
Ernesto U
Viewed
11
shollyman 2020-06-07 01:01:37

我怀疑你只需要提供范围,同时保留现有的服务帐户凭据即可。服务帐户凭据文件未指定范围。根据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

访问令牌意味着你已经通过了身份验证流程并提供了必要的作用域,因此,你既要提供作用域又要提供令牌是没有意义的。你可以使用范围生成令牌,或者将服务帐户用于其他范围。

希望这可以帮助。