[英]`DefaultCredentialsError` when attempting to import google cloud libraries in python
I am attempting to import google-cloud and big-query libraries and running into default credentials
error.我正在尝试导入 google-cloud 和大查询库并遇到
default credentials
错误。 I have attempted to set the credentials by downloading the json file from cloud portal and specifying the path to the file.我试图通过从云门户下载 json 文件并指定文件路径来设置凭据。
## Google Big Query
%reload_ext google.cloud.bigquery
from google.cloud import bigquery
bqclient = bigquery.Client(project = "dat-exp")
os.environ.setdefault("GCLOUD_PROJECT", "dat-exp")
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/xxxxx.json"
---------------------------------------------------------------------------
DefaultCredentialsError Traceback (most recent call last)
/tmp/ipykernel_2944/2163850103.py in <cell line: 81>()
79 get_ipython().run_line_magic('reload_ext', 'google.cloud.bigquery')
80 from google.cloud import bigquery
---> 81 bqclient = bigquery.Client(project = "dat-exp")
82 os.environ.setdefault("GCLOUD_PROJECT", "dat-exp")
DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started
Default Credentials (ADC) is a method of searching for credentials.默认凭据 (ADC) 是一种搜索凭据的方法。
Your code is setting the environment after the client has attempted to locate credentials.在客户端尝试查找凭据后,您的代码正在设置环境。 That means the client failed to locate credentials before you set up credentials.
这意味着客户端在您设置凭据之前无法找到凭据。 A quick solution is to move the line with
bigquery.Client(...)
to be after the os.environ(...)
lines.一个快速的解决方案是将带有
bigquery.Client(...)
的行移动到os.environ(...)
行之后。
os.environ.setdefault("GCLOUD_PROJECT", "dat-exp")
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/xxxxx.json"
bqclient = bigquery.Client(project = "dat-exp")
I do not recommend the method that you are using (modify the environment inside the program).我不推荐您正在使用的方法(修改程序内的环境)。 Either modify the environment before the program starts or specify the credentials to use when creating the client
bigquery.Client()
.在程序启动之前修改环境,或者指定在创建客户端
bigquery.Client()
时要使用的凭据。
from google.cloud import bigquery
from google.oauth2 import service_account
key_path = "path/to/service_account.json"
credentials = service_account.Credentials.from_service_account_file(
key_path, scopes=["https://www.googleapis.com/auth/cloud-platform"])
client = bigquery.Client(credentials=credentials, project='dat-exp')
Provide credentials for Application Default Credentials 为应用程序默认凭据提供凭据
However, the correct method of specifying credentials depends on where you are deploying your code.但是,指定凭据的正确方法取决于您部署代码的位置。 For example, applications can fetch credentials from the compute metadata service when deployed in Google Cloud.
例如,当部署在 Google Cloud 中时,应用程序可以从计算元数据服务中获取凭据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.