|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2015, Google, Inc. |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Command-line application that demonstrates using BigQuery with credentials |
| 17 | +obtained from an installed app. |
| 18 | +
|
| 19 | +This sample is used on this page: |
| 20 | +
|
| 21 | + https://cloud.google.com/bigquery/authentication |
| 22 | +
|
| 23 | +For more information, see the README.md under /bigquery. |
| 24 | +""" |
| 25 | +# [START all] |
| 26 | + |
| 27 | +import argparse |
| 28 | +import pprint |
| 29 | + |
| 30 | +from googleapiclient import discovery |
| 31 | +from googleapiclient.errors import HttpError |
| 32 | +from oauth2client import tools |
| 33 | +from oauth2client.client import AccessTokenRefreshError |
| 34 | +from oauth2client.client import flow_from_clientsecrets |
| 35 | +from oauth2client.file import Storage |
| 36 | + |
| 37 | +SCOPES = ['https://www.googleapis.com/auth/bigquery'] |
| 38 | +# Update with the full path to your client secrets json file. |
| 39 | +CLIENT_SECRETS = 'client_secrets.json' |
| 40 | + |
| 41 | + |
| 42 | +def main(args): |
| 43 | + storage = Storage('credentials.dat') |
| 44 | + credentials = storage.get() |
| 45 | + |
| 46 | + if credentials is None or credentials.invalid: |
| 47 | + flow = flow_from_clientsecrets( |
| 48 | + CLIENT_SECRETS, scope=SCOPES) |
| 49 | + # run_flow will prompt the user to authorize the application's |
| 50 | + # access to BigQuery and return the credentials. |
| 51 | + credentials = tools.run_flow(flow, storage, args) |
| 52 | + |
| 53 | + # Create a BigQuery client using the credentials. |
| 54 | + bigquery_service = discovery.build( |
| 55 | + 'bigquery', 'v2', credentials=credentials) |
| 56 | + |
| 57 | + # List all datasets in BigQuery |
| 58 | + try: |
| 59 | + datasets = bigquery_service.datasets() |
| 60 | + listReply = datasets.list(projectId=args.project_id).execute() |
| 61 | + print('Dataset list:') |
| 62 | + pprint.pprint(listReply) |
| 63 | + |
| 64 | + except HttpError as err: |
| 65 | + print('Error in listDatasets:') |
| 66 | + pprint.pprint(err.content) |
| 67 | + |
| 68 | + except AccessTokenRefreshError: |
| 69 | + print('Credentials have been revoked or expired, please re-run' |
| 70 | + 'the application to re-authorize') |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + parser = argparse.ArgumentParser( |
| 75 | + description=__doc__, |
| 76 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 77 | + # Use oauth2client's argparse as a base, so that the flags needed |
| 78 | + # for run_flow are available. |
| 79 | + parents=[tools.argparser]) |
| 80 | + parser.add_argument( |
| 81 | + 'project_id', help='Your Google Cloud Project ID.') |
| 82 | + args = parser.parse_args() |
| 83 | + main(args) |
| 84 | +# [END all] |
0 commit comments