user-1
(User 1)
October 25, 2022, 6:33pm
1
This topic was automatically generated from Slack. You can find the original thread here .
Liran Galizyan : Trying to add new raw to Big Query using Python with google-cloud library but getting this error: No module named ‘google.cloud’
(I’ve added my code in a response to this thread)
user-1
(User 1)
October 25, 2022, 6:33pm
2
Liran Galizyan : def handler(pd: “pipedream”):
from google.cloud import bigquery
import json
from datetime import datetime, timedelta
content = json.loads(pd.steps[“trigger”][“event”][“body”][“content”])[0][“body”][0][“results”]
campaign_id = content[0][“campaign”][“resourceName”].split(“/”, -1)[3]
leados_id = content[0][“campaign”][“resourceName”]
budget = content[0][“campaign”][“resourceName”]
bid_raw = content[0][“campaign”][“maximizeConversions”][“targetCpa”]
bid = bid_raw[:len (bid_raw) - 6]
account_id = content[0][“campaign”][“resourceName”].split(“/”, -1)[1]
current_date = datetime.now() + timedelta(days=-1)
yesterday_date = current_date.strftime(“%d/%m/%Y”)
start_end = ‘“”"’
body1 = “INSERT INTO lead-os.c_tmr_42069.bid_budget_test(campaign_id,leados_id,budget,bid) VALUES”
data = f”(campaign_id,leados_id,budget,bid)”
query_raw = start_end + body1 + data + start_end
print (query_raw)
GOOGLE_APPLICATION_CREDENTIALS={
“type”: “service_account”,
“project_id”: “lead-os”,
“private_key_id”: “b22adf2fe22f698f444887a5453d60aaf3176456",
“private_key”: “-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBge+eCVTH+pwUee3xIAIuexKUbhTl7FZ\n6Ii0oD7TFCDzFHaMVl/M5xg=\n-----END PRIVATE KEY-----\n”,
“client_email”: “lead-os-pipedream-service-acco@lead-os.iam.gserviceaccount.com ”,
“client_id”: “105192752132652398886",
“auth_uri”: “https://accounts.google.com/o/oauth2/auth” ,
“token_uri”: “https://oauth2.googleapis.com/token” ,
“auth_provider_x509_cert_url”: “https://www.googleapis.com/oauth2/v1/certs” ,
“client_x509_cert_url”: “https://www.googleapis.com/robot/v1/metadata/x509/lead-os-pipedream-service-acco%40lead-os.iam.gserviceaccount.com”
}
# Construct a BigQuery client object.
client = bigquery.Client()
query = query_raw
query_job = client.query(query) # Make an API request.
user-2
(User 2)
October 25, 2022, 6:33pm
3
Dylan Pierce : Hi Liran, have you tried using the special comment import system for Python?
user-2
(User 2)
October 25, 2022, 6:33pm
4
user-2
(User 2)
October 25, 2022, 6:33pm
5
Dylan Pierce : Also, we recommend placing your import statements at the top of the code step, just like a normal Python script
user-1
(User 1)
October 25, 2022, 6:33pm
6
Liran Galizyan : Thanks, pierce!
user-1
(User 1)
October 25, 2022, 6:33pm
7
Liran Galizyan : Now I get this:
user-1
(User 1)
October 25, 2022, 6:33pm
8
Liran Galizyan : Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see S'authentifier auprès de services cloud à l'aide de bibliothèques clientes | Authentification | Google Cloud
DETAILS
No inputs.
user-1
(User 1)
October 25, 2022, 6:33pm
9
Liran Galizyan : I am using the connected apps
user-2
(User 2)
October 25, 2022, 6:33pm
10
Dylan Pierce : You will need to include your Google Application Credentials when you instantiate the client. I would advise placing them within an Environment Variable in your settings:
user-2
(User 2)
October 25, 2022, 6:33pm
11
Dylan Pierce : Then you’ll be able to pass your Google credentials to the BigQuery client
user-1
(User 1)
October 25, 2022, 6:33pm
12
Liran Galizyan : I also tried with environment variable, cant get it to work.
user-1
(User 1)
October 25, 2022, 6:33pm
13
Liran Galizyan : This is the code
user-1
(User 1)
October 25, 2022, 6:33pm
14
Liran Galizyan : This is the result
user-2
(User 2)
October 25, 2022, 6:33pm
16
Dylan Pierce : I think the credentials need to be explicitly passed to the BigQuery constructor as configuration options.
user-2
(User 2)
October 25, 2022, 6:33pm
17
Dylan Pierce : The documentation shows how you can pass specific configuration parameters to the constructor here:
https://googleapis.dev/python/bigquery/latest/usage/client.html
user-1
(User 1)
October 25, 2022, 6:33pm
18
Liran Galizyan : Thanks Pierce, appriciate it. But still doesnt work, same results. Here’s the code:
user-3
(User 3)
October 25, 2022, 6:33pm
19
Dylan Sather (Pipedream) : It looks like you’re passing the project ID, but not the credentials. You’ll see that Google requires that to also be passed in the Client constructor, here: google.cloud.bigquery.client.Client — google-cloud-bigquery documentation
If you’re still not able to get it to work, this would be a great question for the Google team. I believe they do support for Google APIs on Stack Overflow. I’d recommend reaching out there, and they should be able to tell you exactly how to pass the credentials!