我确信这很简单,但找不到任何文档或解决方案。我正在尝试使用gcloud编写脚本,在我的GCP实例中执行某些操作。
是否有方法可以仅通过命令行使用gcloud进行登录/身份验证?
谢谢
我确信这很简单,但找不到任何文档或解决方案。我正在尝试使用gcloud编写脚本,在我的GCP实例中执行某些操作。
是否有方法可以仅通过命令行使用gcloud进行登录/身份验证?
谢谢
你有几个选项(根据你要做什么而定)。
第一个选项是使用--no-launch-browser
选项登录。这仍然需要用户交互,但不需要在你使用的计算机上安装浏览器:
> gcloud auth login --no-launch-browser
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/auth?redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&prompt=select_account&response_type=code&client_id=32555940559.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute&access_type=offline
Enter verification code: *********************************************
Saved Application Default Credentials.
You are now logged in as [user@example.com].
Your current project is [None]. You can change this setting by running:
$ gcloud config set project PROJECT_ID
非交互式选项涉及服务帐号。链接的文档比我能做得更好地解释了它们,但您需要做的简短版本如下:
在Google Developers Console中创建一个服务帐号。确保它具有适当的“作用域”(这些是确定此服务帐户可以执行的操作的权限)。下载相应的JSON密钥文件。
运行gcloud auth activate-service-account --key-file <path to key file>
。
请注意,Google计算引擎VM配备了略有不同的服务帐号;该差异在这里描述。