Posts tagged with python

I'm trying to get the account name and ID of all the Google Ads accounts that the logged user has access to, so the user can later check the stats of each of those accounts.

I've managed to get the IDs using the code on https://developers.google.com/google-ads/api/docs/account-management/listing-accounts?hl=es-419.

But I need the names too and apparently you have to make one API call for each ID to get their account names or any other info.

I've tried with the following Python (Django) code, but it's not working (it probably could be improved a lot and maybe has mistakes, I'm a Python beginner):

def one(request):     client = credenciales(request)     ga_service = client.get_service("GoogleAdsService")     # Get customer resource names from the original code     customer_service = client.get_service("CustomerService")     accessible_customers = customer_service.list_accessible_customers()     customer_resource_names = accessible_customers.resource_names     # Prepare a list to store customer data     list_clients = []     # Iterate through each customer resource name     for resource_name in customer_resource_names:         # Extract the customer ID from the resource name         custom_id = resource_name.split('/')[-1]         # Create a query using the customer_id         query = f'''             SELECT             customer_client.descriptive_name,             customer_client.id,             customer_client.status             FROM customer_client         '''         stream = ga_service.search_stream(customer_id=custom_id, query=query)                  for batch in stream:             for row in batch.results:                 data_clients = {}                 data_clients["descriptive_name"] = row.customer_client.descriptive_name                 data_clients["id"] = row.customer_client.id                 data_clients["status"] = row.customer_client.status                 list_clients.append(data_clients)     # Pass the list of customer data to the template     context = {         'list_clients': list_clients,     }     return render(request, 'one.html', context) 

It gives me the error "authorization_error: USER_PERMISSION_DENIED" (which I don't understand, because it should be automatically showing the accounts that the user has permission to access, I'm not giving those accounts/IDs manually).

Can anyone help make sense of this?

Using google python sdk as an example, according to Google retry policy (https://cloud.google.com/python/docs/reference/storage/latest/retry_timeout):

from google.api_core import exceptions from google.api_core.retry import Retry _MY_RETRIABLE_TYPES = (    exceptions.TooManyRequests,  # 429    exceptions.InternalServerError,  # 500    exceptions.BadGateway,  # 502    exceptions.ServiceUnavailable,  # 503 ) def is_retryable(exc):     return isinstance(exc, _MY_RETRIABLE_TYPES) my_retry_policy = Retry(predicate=is_retryable) 

Why does the following occur when testing is_retryable?

exceptions.TooManyRequests==exceptions.TooManyRequests -> True is_retryable(exceptions.TooManyRequests) -> False  is_retryable(429) -> False  is_retryable(exceptions.TooManyRequests.code) -> False is_retryable(exceptions.TooManyRequests.code.value) -> False  

I have been using a Twilio phone number connected to a WhatsApp Business Account.

As far as I understand, for each conversation started, WhatsApp gives me a 24-hour window where I can send and receive any number of messages without additional charges (WhatsApp Conversation - Free Tier). However, it seems that I am somehow incurring in additional charges for each message sent and received (Messaging Channels Inbound Message and Messaging Channels Outbound Message where each costs $0.005).

I configured the WhatsApp sender to work with a webhook redirecting to an Azure Function App running Python Flask. Despite I can answer an incoming message via a Python Flask, I chose answering via the Twilio REST API (from twilio.rest import Client), since the webhook method gives me only 15 seconds to send an answer or it cuts the connection throwing error 11200, and some of my processes take longer than that.

Are this extra costs because I am using the Twilio REST API or is this a normal behavior of Twilio WhatsApp?

For a week now I have been crawling the web but haven't find any legit documentation to connect the python librairie for Ads API with Service Account.

I want to create a micro-service, server-to-server, so anything else apart service account as authentication is off-table.

On GCP, I was using oAuth2 but I had to manually refresh the tokens with https://developers.google.com/oauthplayground/ which isn't a viable solution.

What I have done so far :

  1. I created a service account
  2. generated a key to it in JSON and downloaded it.
  3. Add all possible access to my service-account permissions, such as :
  • Owner
  • Security Reviewer
  • Service Account Admin
  • Service Account Key Admin
  • Service Account Token Creator
  • Service Account User

4 - Created my script to authenticate :

from google.ads.googleads.client import GoogleAdsClient credentials = {                 "developer_token":  "xXxxxXxxxXxxXXXXxXxxX",                 "use_proto_plus":  true,                 "json_key_file_path":  "./path_to_json.json" ,                 "impersonated_email":  "gcp_and_ads_admin_email",                 "login_customer_id":  "XXXXXXXXXX"             } client = GoogleAdsClient.load_from_dict(credentials) 

When I execute my script, this error follows :

google.auth.exceptions.RefreshError: ('unauthorized_client: Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.', {'error': 'unauthorized_client', 'error_description': 'Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.'})

Does anyone know how I can find a tutorial or help me with this please ?

I follow all step of connectivity to Google Ads API. Using it I get account info like account number and time zone etc. But when I call API for campaign id or ads group at that time it return below output. In ads account (ui) I can see many campaigns.

output :

SearchPager<field_mask { paths: "campaign.id" paths: "ad_group.id"paths: "ad_group.name"}>

additionally : above output is getting in result variable and after that the for loop is not executing in image ofimage of code code