google.cloud.bigquery.Client() 忽略提供的范围,导致在获取 Drive 凭据时权限被拒绝

问题描述

我正在尝试通过 google.cloud.bigquery Python 库查询存储在云端硬盘中的数据。

我已按照 Google 的 Querying Drive Data 指南进行操作。 因此,我的代码如下所示:

import google.auth
from google.cloud import bigquery

credentials,project = google.auth.default(
    scopes=[
        "https://www.googleapis.com/auth/drive","https://www.googleapis.com/auth/bigquery",]
)
client = bigquery.Client(project,credentials)

query = client.query("""MY sql HERE""")
query_results = query.result()

问题是:凭证对象和 bigquery 客户端忽略提供的范围,导致 google.api_core.exceptions.Forbidden: 403 Access Denied: BigQuery BigQuery: Permission denied while getting Drive credentials. 澄清一下,凭证和客户端都不包括提供的驱动器范围。

我该怎么做才能将驱动器范围正确传递给我的 bigquery 客户端?

我的本​​地环境的应用程序认凭据是我的授权用户,即项目的所有者。

解决方法

一旦您将应用程序默认凭据设置为授权用户,您就无法请求其他范围。

要请求其他范围,请在激活您的授权用户时进行。

更简单地说,当您运行 gcloud auth application-default login 时,提供 --scopes 选项,然后是您想要的范围。对我来说,那是 gcloud auth application-default login --scopes=openid,https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/bigquery