Skip to content

Instantly share code, notes, and snippets.

@apstndb
Last active May 28, 2025 15:49
Show Gist options
  • Save apstndb/0b4f3ab2847599d22ead09f66c7547f5 to your computer and use it in GitHub Desktop.
Save apstndb/0b4f3ab2847599d22ead09f66c7547f5 to your computer and use it in GitHub Desktop.
from google.cloud import bigquery
import google.auth
def query():
credentials, project = google.auth.default( scopes=[ "https://www.googleapis.com/auth/bigquery.readonly"] )
client = bigquery.Client(credentials=credentials, project=project)
query = "SELECT 1"
results = client.query(query, api_method=bigquery.enums.QueryApiMethod.QUERY).result()
for row in results:
print(row)
def insert():
# It should fail
credentials, project = google.auth.default( scopes=[ "https://www.googleapis.com/auth/bigquery.readonly"] )
# It should work
# credentials, project = google.auth.default( scopes=[ "https://www.googleapis.com/auth/bigquery"] )
client = bigquery.Client(credentials=credentials, project=project)
query = "SELECT 1"
results = client.query(query, api_method=bigquery.enums.QueryApiMethod.INSERT).result()
for row in results:
print(row)
query()
insert()
(.venv) $ GOOGLE_APPLICATION_CREDENTIALS=${HOME}/Downloads/gsa-cred.json python script.py
Row((1,), {'f0_': 0})
Traceback (most recent call last):
...
google.api_core.exceptions.Forbidden: 403 POST https://bigquery.googleapis.com/bigquery/v2/projects/<my-project>/jobs?prettyPrint=false: Request had insufficient authentication scopes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment