Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OAuth: Starting a Google Compute Instance from within Google App Engine

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.

like image 725
speedplane Avatar asked Jun 30 '13 18:06

speedplane


1 Answers

Here is a complete example of using service accounts and App Engine cron tasks to stop instances after they've been running for a while: (opposite of starting instances, but the authorization code will be the same)

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

AppAssertionCredentials handles the access token using this code:

# 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)
like image 132
Brian Dorsey Avatar answered Oct 24 '22 06:10

Brian Dorsey