ShaharA
ShaharA

Reputation: 903

Using Google Compute API automated over as server

I'm using the Google client API library for Python. My code is running on an Ubuntu 14.04LTS server.

I have a working Google Compute project, were I created and downloaded a OAuth2.0 token to my server.

I'm trying to write a script that does the following:

  1. Automatically (with no user interaction) authenticate to Google Compute engine.
  2. create a new VM and then perform more actions...

My basic problem is using the OAuth2.0 authentication. It required user approval on a javascript supporting browser, and I want to do it automatically, on my server.

Using my code on my desktop works. A browser page pops up requiring my approval. On my server, I get the following message:

we have detected that your javascript is disabled in your browser

The code segment I use for authentication is:

# 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)

I read about service account access as a replacement of the regular OAuth2.0 authentication. Does any one know if that's the best way to go? any thoughts on how to do it better?

Upvotes: 0

Views: 64

Answers (1)

Lennert Kuijpers
Lennert Kuijpers

Reputation: 296

OAuth 2.0 requires user approval and is not the method to go for if you want to run your code/scripts automatically. Service accounts are more suitable for this and are supported by the API (https://cloud.google.com/compute/docs/authentication#tools) You create a service account + key in the developer console and use both to authenticate your application.

Upvotes: 1

Related Questions