从Python客户端访问后端API

时间:2018-06-14 03:57:10

标签: python python-3.x google-app-engine google-cloud-endpoints

我想从Python客户端访问Google App引擎上的我的Endpoints API。我正在使用Google API Python客户端库。 我的API不需要任何身份验证,因此我使用此链接中提供的代码

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?

1 个答案:

答案 0 :(得分:2)

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

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

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

之后我可以在没有身份验证的情况下访问我的端点API。