Skip to content

Instantly share code, notes, and snippets.

@dandelauro

dandelauro/bq.py

Created Dec 7, 2013
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@debnathsinha debnathsinha commented Jul 21, 2014

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