gpt4 book ai didi

google-app-engine - 如何在不使用环境变量的情况下使用凭据访问 Google Datastore API?

转载 作者:行者123 更新时间:2023-12-04 15:52:28 24 4
gpt4 key购买 nike

目前,我正在通过 GOOGLE_APPLICATION_CREDENTIALS 环境变量访问 API。我创建并下载了一个服务帐户 key 作为 .json 文件,并将环境变量设置为该文件。由于我正在进行的项目的要求,我将无法执行此操作。我尝试在数据存储客户端对象的构造函数中使用 credentials 变量,但我无法让它工作。我该怎么办?

我运行的是 Windows 10,但任何解决方案都应该(相对)与操作系统无关。我正在用 python 3.6 编写。

最佳答案

您可以使用 from_service_account_json():

from google.cloud import datastore

client = datastore.Client.from_service_account_json('/path/to/credentials.json')
var = client.get(client.key('MyKind', '<key value>'))
print(var)

参见:https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually

关于google-app-engine - 如何在不使用环境变量的情况下使用凭据访问 Google Datastore API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45288437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com