如何通过 WSL 获得 gcloud auth 打开 Windows 浏览器?

问题描述 投票:0回答:1

尝试使用 google cloud CLI 进行身份验证时,系统会提示您进入 OAuth2 流程,要求您使用 google 帐户登录并批准。

gcloud auth application-default login

但是,这在 Windows 上无法通过 WSL 运行。 google CLI 工具似乎无法识别配置为在主机上运行的浏览器。

流程本身仍然可以工作,因为 CLI 会打印一个可以复制到浏览器的 URL,但作为开发人员,我总是更喜欢偷懒,并尽可能节省更多时间:)

windows-subsystem-for-linux gcloud gcloud-cli
1个回答
0
投票

解决这个问题有两个步骤:

  1. 我们想要为 Linux 发行版设置默认浏览器。这是一种方法 - https://superuser.com/a/1368878
  2. 我们希望
    gcloud
    能够与我们的设置配合使用

深入研究 python CLI 实现后,我在里面找到了这段代码

check_browser.py

# These are environment variables that can indicate a running compositor on
# Linux.
_DISPLAY_VARIABLES = ['DISPLAY', 'WAYLAND_DISPLAY', 'MIR_SOCKET']

....

current_os = platforms.OperatingSystem.Current()
if (current_os is platforms.OperatingSystem.LINUX and
    not any(encoding.GetEncodedValue(os.environ, var) for var
            in _DISPLAY_VARIABLES)):
  launch_browser = False

对我有用的一个简单解决方案是使用环境变量运行 gcloud CLI

DISPLAY='X'
:

DISPLAY='X' gcloud auth application-default login

您还可以设置别名:

alias gauth='DISPLAY="X" gcloud auth application-default login'

但是,这现在可能适用于所有系统。如果代码将来发生变化或者其他人有兴趣以不同的方式对其进行调整,我将相关文件放置在:

/usr/lib/google-cloud-sdk/lib/googlecloudsdk/command_lib/util/check_browser.py

你可以在 python 中使用它

In [21]: from googlecloudsdk.command_lib.util.check_browser import *

In [22]: ShouldLaunchBrowser(True)
Out[22]: False
© www.soinside.com 2019 - 2024. All rights reserved.