FLOW = flow_from_clientsecrets('client_secrets.json',
scope='https://www.googleapis.com/auth/bigquery')
def main():
storage = Storage('bigquery_credentials.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
# Run oauth2 flow with default arguments.
credentials = tools.run_flow(FLOW, storage, tools.argparser.parse_args([]))
http = httplib2.Http()
http = credentials.authorize(http)
bigquery_service = build('bigquery', 'v2', http=http)
try:
query_request = bigquery_service.jobs()
#table = input()
query_data = {"query":"SELECT * FROM [Sample.Sample] order by update_date limit 100000"}
query_response = query_request.query(projectId=PROJECT_NUMBER,
body=query_data).execute()
#textfile = open('text.txt','w')
print ('Query Results:') for row in query_response['rows']: result_row = [] for field in row['f']: result_row.append(field['v']) sys.stdout = open('test.txt','a') output = str(result_row).replace("[", "").replace("]","").replace("'","").encode(encoding='utf_8', errors='strict') print(str(output).replace("b'", "").replace("'", ""))