8000 BigQuery: Use user-based authentication in quickstart. · gnpitty/python-docs-samples@73092e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 73092e6

Browse files
committed
BigQuery: Use user-based authentication in quickstart.
1 parent d2b2619 commit 73092e6

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
google-cloud-bigquery[pandas]==1.9.0
2-
google-auth-oauthlib==0.2.0
3-
ipython==7.2.0
4-
matplotlib==3.0.2
5-
pytz==2018.9
1+
google-cloud-bigquery[pandas]==1.17.0
2+
google-auth-oauthlib==0.4.0
3+
ipython==7.7.0
4+
matplotlib==3.1.1
5+
pytz==2019.2

bigquery/cloud-client/simple_app.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,42 @@
1515
# limitations under the License.
1616

1717
"""Simple application that performs a query with BigQuery."""
18+
1819
# [START bigquery_simple_app_all]
20+
import argparse
21+
1922
# [START bigquery_simple_app_deps]
23+
import google_auth_oauthlib
2024
from google.cloud import bigquery
2125
# [END bigquery_simple_app_deps]
2226

2327

24-
def query_stackoverflow():
28+
def authenticate(client_id, client_secret):
29+
# [START bigquery_simple_app_authenticate]
30+
# TODO(developer): Set the client ID and client secret to the values
31+
# identifying your application.
32+
# client_id = "YOUR-CLIENT-ID.apps.googleusercontent.com"
33+
# client_secret = "abc_ThIsIsAsEcReT"
34+
35+
# This function opens a browser window to authenticate to your Google Cloud
36+
# Platform account. See the Google Cloud Platform Auth Guide for how to
37+
# authenticate your application in non-interactive contexts:
38+
# https://cloud.google.com/docs/authentication/
39+
credentials = google_auth_oauthlib.get_user_credentials(
40+
["https://www.googleapis.com/auth/cloud-platform"],
41+
client_id,
42+
client_secret,
43+
)
44+
# [END bigquery_simple_app_authenticate]
45+
return credentials
46+
47+
48+
def query_stackoverflow(credentials, project_id):
2549
# [START bigquery_simple_app_client]
26-
client = bigquery.Client()
50+
# TODO(developer): Set the project ID to the project used for billing.
51+
# project_id = "my-billing-project-id"
52+
53+
client = bigquery.Client(credentials=credentials, project=project_id)
2754
# [END bigquery_simple_app_client]
2855
# [START bigquery_simple_app_query]
2956
query_job = client.query("""
@@ -41,11 +68,22 @@ def query_stackoverflow():
4168
# [END bigquery_simple_app_query]
4269

4370
# [START bigquery_simple_app_print]
44-
for row in results:
45-
print("{} : {} views".format(row.url, row.view_count))
71+
# Download the full query results as a pandas DataFrame.
72+
df = results.to_dataframe()
73+
print(df)
4674
# [END bigquery_simple_app_print]
4775

4876

77+
def main(project_id, client_id, client_secret):
78+
credentials = authenticate(client_id, client_secret)
79+
query_stackoverflow(credentials, project_id)
80+
81+
4982
if __name__ == '__main__':
50-
query_stackoverflow()
83+
parser = argparse.ArgumentParser("Sample BigQuery Application")
84+
parser.add_argument("project_id")
85+
parser.add_argument("client_id")
86+
parser.add_argument("client_secret")
87+
args = parser.parse_args()
88+
main(args.project_id, args.client_id, args.client_secret)
5189
# [END bigquery_simple_app_all]

bigquery/cloud-client/simple_app_test.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import google.auth
16+
1517
import simple_app
1618

1719

1820
def test_query_stackoverflow(capsys):
19-
simple_app.query_stackoverflow()
21+
credentials, project_id = google.auth.default(
22+
["https://www.googleapis.com/auth/cloud-platform"]
23+
)
24+
simple_app.query_stackoverflow(credentials, project_id)
2025
out, _ = capsys.readouterr()
21-
assert 'views' in out
26+
assert 'stackoverflow.com' in out

0 commit comments

Comments
 (0)
0