python - 使用 google bigquery API 时避免 DefaultCredentialsError

标签 python oauth-2.0 google-api google-bigquery

我正在尝试对某些 bigquery 表执行 SQL 查询。尝试实例化 bigquery 客户端对象时,我不断收到 DefaultCredentialsError 。例如,通过这样做:

from google.cloud import bigquery
client = bigquery.Client.from_service_account_json('service_account_key.json')

或者通过这样做:

from oauth2client.service_account import ServiceAccountCredentials
key = open('service_account_key.json', 'rb').read()

credentials = ServiceAccountCredentials(
    'my_email',
    key,
    scope='https://www.googleapis.com/auth/bigquery')

client = bigquery.Client(credentials=credentials)

我的 .json 凭证文件是否有问题?我创建了一个服务帐户 key :

google api

还有其他建议吗?

最佳答案

您很可能会遇到 bug使用 from_service_account_json 方法。

相反,请尝试使用推荐的身份验证方式,即导出 GOOGLE_APPLICATION_CREDENTIALS 环境变量,如 here 中所述。 .

关于python - 使用 google bigquery API 时避免 DefaultCredentialsError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41385341/

相关文章:

python - 继承具有验证功能的基本 flask 形式

Python:如何在我的包或存储库中安装包?

oauth-2.0 - IdentityServer 中的作用域是什么

c# - 谷歌 Oauth 错误 : At least one client secrets (Installed or Web) should be set

python - 使用 VSCode 连接到 Jupyter 实例

node.js - Google OAuth2 的 RSA 私钥/公钥

javascript - Google 跟踪代码管理器 API 示例在控制台中抛出 "Uncaught RangeError: Maximum call stack size exceeded"错误

python - 值错误 : Client secrets must be for a web or installed app

python - "Error 403: access_denied"来自 Google 身份验证 Web api 尽管 Google 帐户是所有者

python - 如何设置 Tkinter 对话框弹出窗口的位置?