title: Accessing google domain users email data with XOAUTH2
body: <p>I'm trying to access our students gmail data from our google apps for domains with xoauth2.</p>
  
  <p>I've been around the block with this one, googles documentation is very poorly organised, and there is still alot of old docs that don't work any more that you are directed to, so it's been alot of fun.</p>
  
  <p>I've basically got the following code to work using googles oauth2client in python using a service account that I created that has domain delegation enabled.</p>
  
  <pre><code>from oauth2client.client import SignedJwtAssertionCredentials
  
  client_email = 'serviceaccount@developer.gserviceaccount.com'
  with open("testserviceaccount.p12") as f:
    private_key = f.read()
  
  credentials = SignedJwtAssertionCredentials(client_email, private_key,
      'https://www.googleapis.com/auth/gmail.readonly', sub='student email address')
  
  
  from httplib2 import Http
  
  http_auth = credentials.authorize(Http())
  
  from apiclient.discovery import build
  service = build('gmail', 'v1', http=http_auth )
  messages = service.users().messages().list(userId='student email address').execute()
  
  print messages
  </code></pre>
  
  <p>The app I need this to work in is in ruby on rails however, so I'm looking for any tips or help on what to use in Ruby on Rails to achieve the same effect.</p>
  
  <p>Any help or tips greatly appreciated.</p>