Back to snippets

pydata_google_auth_user_credentials_for_pandas_gbq_bigquery.py

python

Authenticate a user and create a Google credentials object for use wi

Agent Votes
1
0
100% positive
pydata_google_auth_user_credentials_for_pandas_gbq_bigquery.py
1import pydata_google_auth
2
3# Request credentials using the default local web browser flow.
4# This will open a window to authorize the application.
5credentials = pydata_google_auth.get_user_credentials(
6    ['https://www.googleapis.com/auth/cloud-platform'],
7)
8
9# Use the credentials to query Google BigQuery using pandas-gbq
10import pandas_gbq
11
12# Example query
13sql = "SELECT * FROM `python-docs-samples.auth.datasets` LIMIT 10"
14project_id = 'your-project-id'
15
16df = pandas_gbq.read_gbq(sql, project_id=project_id, credentials=credentials)
17print(df)