【问题标题】:Using Google Compute API automated over as server使用 Google Compute API 自动作为服务器
【发布时间】:2016-03-03 00:41:12
【问题描述】:

我正在使用适用于 Python 的 Google 客户端 API 库。我的代码在 Ubuntu 14.04LTS 服务器上运行。

我有一个正在运行的 Google Compute 项目,我是否已创建 OAuth2.0 令牌并将其下载到我的服务器。

我正在尝试编写一个执行以下操作的脚本:

  1. 自动(无需用户交互)向 Google Compute 引擎进行身份验证。
  2. 创建一个新 VM,然后执行更多操作...

我的基本问题是使用 OAuth2.0 身份验证。它需要用户在支持 javascript 的浏览器上批准,我想在我的服务器上自动完成。

在我的桌面上使用我的代码有效。弹出一个浏览器页面,需要我的批准。在我的服务器上,我收到以下消息:

我们检测到您的 javascript 在您的浏览器中被禁用

我用于认证的代码段是:

# authenticate using the OAuth token                                
client_secret = os.path.join(
     os.path.dirname(__file__),
     self._oauth_token_path)
# set up a Flow object for the authentication                                           
flow = client.flow_from_clientsecrets(
     client_secret,
     scope=scope,
     message=tools.message_if_missing(client_secret))
# open credential storage path                                                          
credential_storage = file.Storage(self._credential_storage_path)
credentials = credential_storage.get()
# get credentails if necessary                                                          
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, credential_storage, flags)

我阅读了有关服务帐户访问以替代常规 OAuth2.0 身份验证的信息。有谁知道这是否是最好的方法?有什么想法可以做得更好吗?

【问题讨论】:

    标签: google-compute-engine google-cloud-platform google-api-python-client


    【解决方案1】:

    OAuth 2.0 需要用户批准,如果您想自动运行您的代码/脚本,则不适合这种方法。 服务账号更适合这个,API 支持(https://cloud.google.com/compute/docs/authentication#tools) 您在开发者控制台中创建一个服务帐户 + 密钥,并使用两者来验证您的应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 2013-11-14
      • 2018-07-11
      • 1970-01-01
      相关资源
      最近更新 更多