【发布时间】:2016-03-03 00:41:12
【问题描述】:
我正在使用适用于 Python 的 Google 客户端 API 库。我的代码在 Ubuntu 14.04LTS 服务器上运行。
我有一个正在运行的 Google Compute 项目,我是否已创建 OAuth2.0 令牌并将其下载到我的服务器。
我正在尝试编写一个执行以下操作的脚本:
- 自动(无需用户交互)向 Google Compute 引擎进行身份验证。
- 创建一个新 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