Python 使用google bigquery API时避免DefaultCredentialsError

Python 使用google bigquery API时避免DefaultCredentialsError,python,oauth-2.0,google-api,google-bigquery,Python,Oauth 2.0,Google Api,Google Bigquery,我正在尝试对某个bigquery表执行SQL查询。在尝试实例化bigquery客户端对象时,我不断收到一个DefaultCredentialError。例如,通过这样做: from google.cloud import bigquery client = bigquery.Client.from_service_account_json('service_account_key.json') from oauth2client.service_account import ServiceAc

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

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)
或者通过这样做:

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 credentals文件可能有问题吗?我创建了一个服务帐户密钥:


还有其他建议吗?

您最有可能通过使用
from\u service\u account\u json
方法遇到问题


相反,请尝试使用推荐的身份验证方法,方法是导出所述的
GOOGLE\u应用程序\u凭据
环境变量。

您是否尝试过使用(推荐的)
GOOGLE\u应用程序\u凭据
身份验证方法?请参见此处:。你能测试一下这是否有效吗?你可能遇到了这个错误-谢谢@GrahamPolley。通过导出GOOGLE_应用程序_凭据位置,我能够使用来自_服务_帐户_json的
实例化客户端。考虑到我将位置直接从_service_account_json传递到
函数,我不得不这样做是不寻常的。这是个好消息@AlexG。我现在就发布一个答案,以防其他人遇到与你相同的问题。同时,也许你想在这里提出一个问题->并记住这可能是由于死链接。现在可以阅读有关更新的链接。非常感谢。