我有一个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表没有问题,但是我不明白我在这里做错了什么。感谢您的帮助。
答案 0 :(得分:0)
# 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
答案 1 :(得分:0)
好的,我终于明白了。我以为我已经获得了访问服务帐户电子邮件的权限,但是似乎由于我不是该文件的所有者而无法使用。使用高级访问请求也不会向所有者发送请求。
所以我要求所有者向服务帐户电子邮件发送邀请,现在一切正常。