使用 python 导入 GCP 资源时出现凭据错误

Credentials error while using python to import GCP resources

我是编程新手,正在尝试使用 python 导入现有的 GCP 资源。但是我在代码本身的开头遇到了这个特殊的“默认凭据错误”。感谢任何帮助。

Exception has occurred: 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
  File "C:\Users\dilee\Desktop\Python\GCP\start.py", line 4, in <module>
    publisher = pubsub_v1.PublisherClient()

第二个错误

Exception has occurred: DefaultCredentialsError
File "C:\Users\dilee\Desktop\PythonGCP\learning-project-311506-1b36ea6ccc86.json" was not found.
  File "C:\Users\dilee\Desktop\PythonGCP\start.py", line 6, in <module>
    publisher = pubsub.PublisherClient()

Google Cloud API 实际上需要一个密钥才能知道您使用的是哪个 google 帐户以及您的权利是什么。此密钥采用凭据形式。

基本上,您要做的就是先验证您自己的身份。 如果您在本地编码,最好的方法是先键入 gcloud auth application-default login(如果您想设置默认登录名)或仅键入 gcloud auth login(但只有当前使用的命令行将被识别为 google云)。

那么您就可以正常使用 GCP API :)

如果您需要更多详细信息,请参阅 Here