从命令行登录GCP

Lio*_*ana 14 gcloud

我确信这很简单但找不到任何文件或解决方案.我正在尝试使用gcloud编写脚本来在我的GCP实例中执行某些操作.

无论如何只能通过命令行使用gcloud登录/验证?

谢谢

Zac*_*man 25

你有几个选择(取决于你究竟想做什么).

第一个选项是使用该--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
Run Code Online (Sandbox Code Playgroud)

非交互选项涉及服务帐户.链接的文档比我更好地解释了它们,但您需要做的简短版本如下:

  1. Google Developers Console中创建服务帐户.确保它具有适当的"范围"(这些是确定此服务帐户可以执行的操作的权限.下载相应的JSON密钥文件.

  2. gcloud auth activate-service-account --key-file <path to key file>.

请注意,Google Compute Engine虚拟机的服务帐户略有不同; 差异在这里描述.