如何在本地使用Google Cloud Platform客户端库时确定身份验证方法

时间:2018-01-30 17:28:47

标签: python authentication google-cloud-platform gcloud-python vision-api

我目前能够运行使用python client library调用Google Vision API的本地python脚本(具体来说,我使用export GOOGLE_APPLICATION_CREDENTIALS = path/to/JSON/key/file包)。但是,我很好奇它是如何进行身份验证的。在我在本地运行的python脚本中,我没有提供任何身份验证信息。通过阅读以下帖子,似乎在本地运行时进行身份验证的常用方法是将环境变量设置为.JSON密钥文件的路径(即printenv),但是,我不记得这样做,如果我运行const handler = { construct(objTarget, args, oldConstructor) { return new objTarget(...args) }, set(target, key, value) { console.log(`Setting value ${key} as ${value}`) target[key] = value; }, get(target, key) { console.log(`Reading value from ${key}`) return target[key]; }, }; function wand (args){console.log(args)} const Wand = new Proxy(wand, handler); var w = new Wand(["some","args"]); ,我没有名为GOOGLE_APPLICATION_CREDENTIALS的环境变量。

以下帖子提供了有关在本地使用客户端库进行身份验证的不同方法的详细信息,但如何查看/确定我的程序的身份验证方式?有没有办法查询?

"Authenticating to the Cloud Vision API" ...包括上页的"Application Default Credentials"部分

"Authenticating Applications With a Client Library"创建和启用实例服务帐户

的部分

"Providing Credentials to Your Application"部分"设置服务器到服务器生产能力的身份验证"页

"Setting the Environment Variable""身份验证入门"页:

Python客户端库"Getting Started"页面:

"Authenticating to a Cloud API Service"

1 个答案:

答案 0 :(得分:0)

在不创建credentials object的情况下,可以通过4种不同的方式对请求进行身份验证。

  1. 如果环境变量GOOGLE_APPLICATION_CREDENTIALS设置为有效服务帐户JSON私钥文件的路径,则使用该文件。
  2. 如果已安装Google Cloud SDK且设置了application default credentials,则会使用它。请注意,如果您在过去曾执行过此步骤,则该步骤将保持有效。 (我猜这是您目前用于验证的内容。)
  3. 如果应用程序在App Engine标准环境中运行,则使用来自App Identity Service的凭据和项目ID。 (此处不适用,但为了完整性而我列出了它。)
  4. 如果应用程序在Compute Engine或App Engine灵活环境中运行,则从元数据服务获取凭据和项目ID。 (此处不适用,但我也列出了完整性和清单。)
  5. 如果使用上述方法找不到凭据,则会引发DefaultCredentialsError。由于您没有收到此错误,并且您没有#1设置的环境变量,以及选项#3& #4不适用,剩下的唯一选项是#2号。

    上述信息可以在readthedocs.io page for the google-cloud Authentication page上找到,更具体地说,可以在google.auth package page

    中找到

    您可以通过运行此command

    来检查是否设置了应用程序默认凭据
    gcloud auth application-default print-access-token 
    

    如果这不会返回错误而是访问令牌,则表示已设置#2。 当然不要与任何人分享此令牌......

    一些相关信息,您可以使用上面here命令检查打印出的令牌,或使用下面的curl命令(在末尾粘贴令牌):

    curl -i https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=
    

    这并没有完全回答你的问题,但是通过消除过程它应该是正确的...