从 Python 个客户端访问后端 API

Accessing Backend APIs from Python Clients

我想从 Python 客户端访问 Google 应用引擎上的端点 API。我正在使用 Google APIs Python 客户端库。 我的 API 不需要任何身份验证,因此我正在使用此 link

中提供的代码

https://cloud.google.com/endpoints/docs/frameworks/python/access_from_python

但是我收到这个错误:

File "/usr/local/lib/python3.5/dist-packages/googleapiclient/discovery.py", line 358, in build_from_document
  credentials = _auth.default_credentials()
File "/usr/local/lib/python3.5/dist-packages/googleapiclient/_auth.py", line 42, in default_credentials
credentials, _ = google.auth.default()
File "/usr/local/lib/python3.5/dist-packages/google/auth/_default.py", line 306, 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://developers.google.com/accounts/docs/application-default-credentials.

我需要设置哪些凭据才能成功进行 API 调用?

我们需要在 GCP 控制台中创建服务帐户密钥。更多细节在这里: https://cloud.google.com/storage/docs/reference/libraries

然后在python代码中我们添加:

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/SERVICE_KEY.json"

之后,我无需身份验证即可访问我的端点 API。