我正在设置一个具有互联网访问权限的windows 10 vm (我在vm内的chrome上很好地浏览互联网)。我下载并安装了google。我运行了gcloud init,并设置了我的帐户和其他选项。它说它成功地完成了。我输入gcloud info并显示正确的信息。问题是,当我运行一个包含from google.cloud import datastore的python脚本时。它与帐户身份验证有问题。只有当我尝试实例化数据存储客户端时才会发生此错误:client = datastore.Client()。这当然有道理。但是,因为我已经执行了gcloud init并使用gcloud信息验证了它,所以这里不需要设置什么?
此外,看起来google库无法正确地向某个服务器发出请求。下面是日志输出:
2020-09-27 20:26:30 [google.auth._default] DEBUG: Checking None for explicit credentials as part of auth process...
2020-09-27 20:26:30 [google.auth._default] DEBUG: Checking Cloud SDK credentials as part of auth process...
2020-09-27 20:26:30 [google.auth._default] DEBUG: Cloud SDK credentials not found on disk; not using them
2020-09-27 20:26:30 [google.auth._default] DEBUG: Checking for App Engine runtime as part of auth process...
2020-09-27 20:26:30 [google.auth._default] DEBUG: No App Engine library was found so cannot authentication via App Engine Identity Credentials.
2020-09-27 20:26:30 [google.auth.transport._http_client] DEBUG: Making request: GET http://169.254.169.254
2020-09-27 20:26:33 [google.auth.compute_engine._metadata] WARNING: Compute Engine Metadata server unavailable onattempt 1 of 3. Reason: timed out
2020-09-27 20:26:33 [google.auth.transport._http_client] DEBUG: Making request: GET http://169.254.169.254
2020-09-27 20:26:36 [google.auth.compute_engine._metadata] WARNING: Compute Engine Metadata server unavailable onattempt 2 of 3. Reason: timed out
2020-09-27 20:26:36 [google.auth.transport._http_client] DEBUG: Making request: GET http://169.254.169.254
2020-09-27 20:26:39 [google.auth.compute_engine._metadata] WARNING: Compute Engine Metadata server unavailable onattempt 3 of 3. Reason: timed out
2020-09-27 20:26:39 [google.auth._default] WARNING: Authentication failed using Compute Engine authentication due to unavailable metadata server.调用堆栈末尾的实际错误是:
File "C:\Users\<user>\anaconda3\lib\site-packages\google\auth\_default.py", line 354, in default
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.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编辑:注意,日志中列出的这个IP地址在这里提到:https://serverfault.com/questions/427018/what-is-this-ip-address-169-254-169-254/427022,它看起来是一个本地的唯一ip地址。但是这很奇怪,因为这个VM有一个DHCP分配的IP地址,甚至与主机IP不一样。我在用虚拟盒。我尝试过两种网络类型设置:桥接适配器和NAT。两个人都不在这里工作。这是VM网络的错误还是其他我没有意识到的?
发布于 2020-09-28 11:45:34
我认为在这种情况下,您应该按照错误消息添加GOOGLE_APPLICATION_CREDENTIALS,并将路径添加到私钥中。如何获得密钥是在链接的错误。
在Datastore文档从另一个平台访问数据库中也可以找到相同的方法。
在“如何指导开始使用Datastore模式API中的Firestore”中,包含与您类似的示例,您将发现:
在本例中,您将从环境中获取应用程序默认凭据,并将其作为参数传递以创建服务对象。
登录到gcloud并不意味着您总是在特定的计算机上进行身份验证。当您运行python脚本时,您运行的应用程序与gcloud不同。这意味着您必须进行身份验证。
发布于 2020-12-02 17:05:01
对我来说,(因为这是一台新机器,而且我已经在上一台机器上使用了所有python代码和gcp帐户设置)。问题是在gcloud init之后,在我的东西开始工作之前,我也需要运行gcloud auth application-default login。
https://stackoverflow.com/questions/64095597
复制相似问题