访问由电子表格支持的bigquery表时出现BadRequest / ForbiddenException异常
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了访问由电子表格支持的bigquery表时出现BadRequest / ForbiddenException异常相关的知识,希望对你有一定的参考价值。
我有一个bigquery表,其数据源是google电子表格。我正在尝试使用python访问此表,但遇到以下错误google.api_core.exceptions.BadRequest: 400 Error while reading table: datset.table, error message: Failed to read the spreadsheet. Errors: com.google.apps.framework.request.ForbiddenException: Permission denied [S]#RITZ#369137407806#topTeRwnQcmrdMeVQ98ZkGA
我的应用已通过具有BigQuery管理员角色的ServiceAccount JSON令牌进行身份验证,这是创建我的客户端的方式:
from google.oauth2.service_account import Credentials
import os
scopes = [
'https://www.googleapis.com/auth/bigquery',
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/drive',
]
credentials = Credentials.from_service_account_file(os.environ['GOOGLE_APPLICATION_CREDENTIALS'], scopes=scopes)
client = bigquery.Client(credentials=credentials)
我对严格的BigQuery表没有问题,但是我无法理解我在这里做错了什么。感谢您的帮助。
# from google.cloud import bigquery
# client = bigquery.Client()
# dataset_id = 'my_dataset'
dataset_ref = client.dataset(dataset_id)
job_config = bigquery.LoadJobConfig()
job_config.schema = [
bigquery.SchemaField("name", "STRING"),
bigquery.SchemaField("post_abbr", "STRING"),
]
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
uri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"
load_job = client.load_table_from_uri(
uri,
dataset_ref.table("us_states"),
location="US", # Location must match that of the destination dataset.
job_config=job_config,
) # API request
print("Starting job {}".format(load_job.job_id))
load_job.result() # Waits for table load to complete.
print("Job finished.")
destination_table = client.get_table(dataset_ref.table("us_states"))
print("Loaded {} rows.".format(destination_table.num_rows))
我从Google那里获得的上述代码对我来说很好用。他们确实有其他语言的代码,请检查此https://cloud.google.com/bigquery/docs/loading-data-cloud-storage-json
以上是关于访问由电子表格支持的bigquery表时出现BadRequest / ForbiddenException异常的主要内容,如果未能解决你的问题,请参考以下文章
访问 Google Sheets 联合表时出现 BigQuery 凭据问题
从 Google Apps 脚本访问 GoogleSheet 链接的 BigQuery 表时出现“获取驱动器凭据时权限被拒绝”错误
尝试从 BigQuery 读取表并使用 Airflow 将其保存为数据框时出现 _pickle.PicklingError