OAuth:从 Google App Engine 中启动 Google 计算实例

2024-05-01

我有一个 Google App Engine 网络应用程序,它运行着我网站的大部分内容。然而,对于某些功能,我需要一台linux机器。我希望我的 Google App Engine 应用程序能够在某些事件上自动启动 Google 计算实例。

据我了解,您可以使用 Compute Engine REST API 添加 Google Compute 实例。但是,为了访问 Google Compute REST API,您需要使用 OAuth2 身份验证过程获取访问令牌。

如何以编程方式从 Google App Engine 中获取访问令牌?

似乎所有身份验证方法都需要出现一个窗口,以便您可以输入用户名和密码,这在 Google App Engine 中是不切实际的。


以下是使用服务帐户和 App Engine cron 任务在实例运行一段时间后停止实例的完整示例: (与启动实例相反,但授权码相同)

https://github.com/GoogleCloudPlatform/compute-appengine-timeout-python https://github.com/GoogleCloudPlatform/compute-appengine-timeout-python

AppAssertionCredentials 处理访问令牌使用此代码 https://github.com/GoogleCloudPlatform/compute-appengine-timeout-python/blob/master/main.py#L49:

# Obtain App Engine AppAssertion credentials and authorize HTTP connection.
# https://developers.google.com/appengine/docs/python/appidentity/overview
credentials = AppAssertionCredentials(
    scope='https://www.googleapis.com/auth/compute')
HTTP = credentials.authorize(httplib2.Http(memcache))

# Build object for the 'v1beta15' version of the GCE API.
# https://developers.google.com/compute/docs/reference/v1beta13/
compute = build('compute', 'v1beta15', http=HTTP)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OAuth:从 Google App Engine 中启动 Google 计算实例 的相关文章

随机推荐