Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8779289

Browse filesBrowse files
author
Jon Wayne Parrott
committed
Adding bigquery installed app auth sample.
1 parent fdbe26d commit 8779289
Copy full SHA for 8779289

File tree

Expand file treeCollapse file tree

3 files changed

+131
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+131
-0
lines changed

‎.gitignore

Copy file name to clipboardExpand all lines: .gitignore
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
coverage.xml
66
python-docs-samples.json
77
service-account.json
8+
client-secrets.json
89
__pycache__
910
*db\.sqlite3
1011
managed_vms/django_tutorial/static/*
@@ -16,3 +17,4 @@ testing/resources/client-secrets.json
1617
secrets.tar
1718
.cache
1819
junit.xml
20+
credentials.dat

‎bigquery/api/installed_app.py

Copy file name to clipboard
+84Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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]

‎bigquery/api/installed_app_test.py

Copy file name to clipboard
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import re
15+
16+
import installed_app
17+
from oauth2client.client import GoogleCredentials
18+
19+
20+
class Namespace(object):
21+
def __init__(self, **kwargs):
22+
self.__dict__.update(kwargs)
23+
24+
25+
def test_main(cloud_config, monkeypatch, capsys):
26+
installed_app.CLIENT_SECRETS = cloud_config.client_secrets
27+
28+
# Replace the user credentials flow with Application Default Credentials.
29+
# Unfortunately, there's no easy way to fully test the user flow.
30+
def mock_run_flow(flow, storage, args):
31+
return GoogleCredentials.get_application_default()
32+
33+
monkeypatch.setattr(installed_app.tools, 'run_flow', mock_run_flow)
34+
35+
args = Namespace(
36+
project_id=cloud_config.project,
37+
logging_level='INFO',
38+
noauth_local_webserver=True)
39+
40+
installed_app.main(args)
41+
42+
out, _ = capsys.readouterr()
43+
44+
assert re.search(re.compile(
45+
r'bigquery#datasetList', re.DOTALL), out)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.