Skip to content

Instantly share code, notes, and snippets.

@dandelauro
Created December 7, 2013 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dandelauro/7836962 to your computer and use it in GitHub Desktop.
Save dandelauro/7836962 to your computer and use it in GitHub Desktop.
app engine / bigquery server to server authentication through service account
import httplib2
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
#
PROJECT_ID = '############'
SERVICE_ACCOUNT_EMAIL = '############-##################@developer.gserviceaccount.com'
SCOPE = 'https://www.googleapis.com/auth/bigquery'
P12_CERT = 'PATH/TO/###########################-privatekey.p12'
#
cert = file(P12_CERT, 'rb')
key = cert.read()
cert.close()
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL,key,scope=SCOPE)
http = httplib2.Http()
http = credentials.authorize(http)
service = build('bigquery', 'v2', http=http)
query = {
'query':'SELECT TOP(title, 10) as title, COUNT(*) as revision_count FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;',
'timeoutMs':10000
}
job = service.jobs()
result = job.query(projectId=PROJECT_ID,body=query).execute()
print "results are in!"
for row in result['rows']:
result_row = []
for field in row['f']:
result_row.append(field['v'])
print ('\t').join(result_row)
@debnathsinha
Copy link

Did this work on the local dev_appserver as well? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment