简体   繁体   English

尝试在 python 中导入谷歌云库时出现“DefaultCredentialsError”

[英]`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.

相关问题 Cloud Run 上的 Flask web 应用程序 - google.auth.exceptions.DefaultCredentialsError: - Flask web app on Cloud Run - google.auth.exceptions.DefaultCredentialsError: google.auth.exceptions.DefaultCredentialsError: - google.auth.exceptions.DefaultCredentialsError: Python 和 HTTP Google Cloud Functions 上的“导入请求” - Python and "import requests" on HTTP Google Cloud Functions ContextualVersionConflict,在 AWS SageMaker 上尝试 pip install google-cloud-bigquery 时 - ContextualVersionConflict, when attempting to pip install google-cloud-bigquery on AWS SageMaker 如何使用 google.cloud 修复 python“未解决的导入”错误 - How to fix python "unresolved import" -error with google.cloud 为什么在尝试 docker 从谷歌云 us-docker.pkg.dev 中拉取时“服务不可用” - Why "service not available" when attempting docker pull from google cloud us-docker.pkg.dev ImportError:无法为 Python 导入 Cloud Firestore 库。确保安装“google-cloud-firestore”模块 - ImportError: Failed to import the Cloud Firestore library for Python. Make sure to install the "google-cloud-firestore" module Google Cloud - 错误报告客户端库 - Google Cloud - Error Reporting Client Libraries Google Cloud Platform 在使用第三方库时出现 PEM 错误 - Google Cloud Platform gives PEM Error when using third party libraries Google Cloud SQL 中的导入是否有限制? - Is there a limit on the import in Google Cloud SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM