I want to load and write data on my GBQ. I tried to use read_gbq()
and followed the instructions for the authenticating. I used my service account for authentication. I referred to the official documentation of pandas_gbq
shown below:
from google.oauth2 import service_account
import pandas_gbq
credentials = service_account.Credentials.from_service_account_file(
'path/to/key.json',
)
df = pandas_gbq.read_gbq(sql, project_id="YOUR-PROJECT-ID", credentials=credentials)
This throws an error. This is the error I am getting:
AttributeError: module 'google.auth.credentials' has no attribute 'CredentialsWithTokenUri'
I then tried to use the BigQuery Client for Python. I used the BigQuery documentation to authenticate. This is what I did:
from google.cloud import bigquery
from google.oauth2 import service_account
key_path = 'creds/my-cred-file.json'
credentials = service_account.Credentials.from_service_account_file(key_path)
client = bigquery.Client(credentials=credentials, project=credentials.project_id)
When I ran the query through:
query_job = client.query(sql)
This returns the data where I can iterate through. So, the data from the tables are being read using this method. But when I tried this:
query_job.to_dataframe()
It throws me an error, PermissionDenied: 403 request failed: the user does not have 'bigquery.readsessions.create' permission for 'projects/my-project-staging'
.
I want to know if something is wrong with my service account. I feel like it shouldn't be, because I am able to read data by iterating through query_job
variable. Is there any sort of documentation that would help me?
I tried to read and load data from GBQ. I was expecting I could easily turn the result into pandas DataFrame.