@David answer got me on the right track, but I had some problems with using a service account.
Solution
I decided to use service account and impersonation API, beware that this gives a very high level of access to your applicaiton and raises the security bar consideably!
Anyways, here is what you need to do:
Set up a service account
Follow this tutorial, but when setting up credentials create a service account
Here is the revelant part of page:
(...) you can activate the Admin SDK yourself in the Developers
Console by doing the following:
- Go to the Google Developers Console.
- Select a project, or create a new one.
- In the sidebar on the left, expand APIs & auth. Next, click APIs. In the list of APIs, make sure the status is ON for the Admin SDK.
- In the sidebar on the left, select Credentials.
- In either case, you end up on the Credentials page and can create your project's credentials from here.
If you haven't done so already, create your OAuth 2.0 credentials by
clicking Create new Client ID under the OAuth heading. Next, look for
your application's client ID and client secret in the relevant table
You may also create and edit redirect URIs from this page.
Domain wide delegation of authority
Perform a domain wide delegation of authority to your code, using this guide..
- Go to your Google Apps domain’s Admin console.
- Select Security from the list of controls. If you don't see Security listed, select More controls from the gray bar at the bottom of the
page, then select Security from the list of controls.
- Select Advanced settings from the list of options.
- Select Manage third party OAuth Client access in the Authentication section.
- In the Client name field enter the service account's Client ID.
- In the One or More API Scopes field enter the list of scopes that your application should be granted access to (see image below). For
example if you need domain-wide access to the Google Drive API and the
Google Calendar API enter: https://www.googleapis.com/auth/drive,
https://www.googleapis.com/auth/calendar
Click the Authorize button.
Please note that you'll need to provide Client ID
not Email Address
in step 5.
See this for list of scopes.
Install required dependencies
Install google-api-python-client, PyCrypto an PyOpenSSL (you may omit PyOpenSSL), but then you'll need to convert downloaded certificate.
You can use following sample to perform authentication
with open('private/key-filename.p12', 'rb') as f:
private_key = f.read()
credentials = SignedJwtAssertionCredentials(
'user-email-@developer.gserviceaccount.com', # Email address [1]
private_key,
'https://www.googleapis.com/auth/admin.directory.user',
sub="impersonated-user@foo.bar" # Impersonate user [2])
- Where 1 is an service account e-mail address, and 2 is an already existing address of admin user in your domain. From now on all actions taken by the API will be performed using this (marked by 2) user authorisation and credentials.
- Note that 1 contains
Email Address
of Service account (this is different from Client ID) in step 5.
Now you should have read-write account to your API.