OAuth:在Google App Engine中启动Google Compute实例 [英] OAuth: Starting a Google Compute Instance from within Google App Engine

查看:157
本文介绍了OAuth:在Google App Engine中启动Google Compute实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google App Engine网络应用程序,可以运行我的大部分网站。但是,对于某些功能,我需要一台Linux机器。我希望我的Google App Engine应用程序在某些事件中自动启用Google Compute Instance。

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



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



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

>您应该能够使用与您的项目相关联的服务帐户对Compute Engine API进行身份验证并启动虚拟机。
$ b 有关服务帐户的文档表明以下Python代码应该获取服务帐户令牌。

 导入httplib2 

从oauth2client.appengine导入发现
导入AppAssertionCredentials
...
凭证= AppAssertionCredentials(
scope ='https://www.googleapis.com/auth/compute')
auth_http = cred entials.authorize(httplib2.Http())
compute_service = discovery.build('compute','v1beta15',http = auth_http)

我认为今年的Google I / O演示会在他们构建视频共享网站的位置提供,但我在GitHub上看不到它。有许多使用AppEngine来控制GCE的大量演示,但大多数他们似乎使用用户的项目和凭据,而不是应用程序自己的凭据。



很显然,您可能不希望直接用户输入旋转虚拟机,除非你有一个非常大的预算或某种形式的速率限制,但是当你需要做大量的计算时,时不时会启动一个VM。 (转码等)

I have a Google App Engine web app that runs the majority of my site. However, for certain functions, I need a linux machine. I would like my Google App Engine app to automatically spin-up a Google Compute Instance on certain events.

I understand that you can add Google Compute instances using the Compute Engine REST API. However, in order to access the Google Compute REST API, you need to get an access token using the OAuth2 authentication process.

How can I programmatically get an access token from within Google App Engine?

It seems that all of the authentication methods require a window to appear so you can type in your username and password, which is impractical from within Google App Engine.

解决方案

You should be able to use the service account associated with your project to authenticate to the Compute Engine API and launch VMs.

Documentation on service accounts suggests that the following python code should fetch a service account token.

import httplib2

import discovery
from oauth2client.appengine import AppAssertionCredentials
...
credentials = AppAssertionCredentials(
    scope='https://www.googleapis.com/auth/compute')
auth_http = credentials.authorize(httplib2.Http())
compute_service = discovery.build('compute', 'v1beta15', http=auth_http)

I'd thought that the Google I/O demo from this year where they built a video-sharing site was going to be available, but I don't see it on GitHub yet. There are a number of demos that use AppEngine to control GCE, but most of them seem to use the user's project and credentials, rather the app's own credentials.

Obviously, you probably don't want to spin up a VM on direct user input unless you've got a very large budget or some form of rate limiting in place, but it's quite helpful to spin up a VM now and then when you've got a lot of computation to do. (Transcoding, etc.)

这篇关于OAuth:在Google App Engine中启动Google Compute实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆