我尝试这样做,但出现错误
import os
creds_file_path = os.path.expanduser("~/.config/gcloud/credentials")
if os.path.isfile(creds_file_path):
print("You are authenticated with gcloud auth login.")
else:
print("You are not authenticated with gcloud auth login.")
这给了
No such file or directory
有没有其他不使用外部库的方法?
如果您想创建访问 Google Cloud 的 Python 脚本,您只需将以下环境变量设置为您的服务帐户 Json 密钥文件路径:
export GOOGLE_APPLICATION_CREDENTIALS=/your_folder/sa_key.json
然后您可以启动包含 Python 脚本入口点的主文件。
虽然不是 Python,但这里有一些可能有帮助的信息。
给定一个token,您可以通过API获取token信息,例如:
curl -H "Content-Type: application/x-www-form-urlencoded" \
-d "access_token=$(gcloud auth print-access-token)" \
https://www.googleapis.com/oauth2/v1/tokeninfo
这将返回一个具有
expires_in
值的 json 结构:
{
"issued_to": "xxxxxxxxxxx.apps.googleusercontent.com",
"audience": "xxxxxxxxxxx.apps.googleusercontent.com",
"user_id": "000000000000000000000",
"scope": "https://www.googleapis.com/auth/accounts.reauth openid etc",
"expires_in": 1112,
"email": "[email protected]",
"verified_email": true,
"access_type": "offline"
}
但是,目前还不清楚当
expires_in
为0时,gcloud是否会自动续订。