我目前能够运行使用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"页面:
答案 0 :(得分:0)
在不创建credentials object的情况下,可以通过4种不同的方式对请求进行身份验证。
如果使用上述方法找不到凭据,则会引发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=
这并没有完全回答你的问题,但是通过消除过程它应该是正确的...