Last active
May 28, 2025 15:49
-
-
Save apstndb/0b4f3ab2847599d22ead09f66c7547f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(.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