Posts tagged with python

I'm pretty new to python and Flask development. I'm primarily a frontend JavaScript/React developer.

I am building a tool that uses the Google Ads api, and their documentation had me create a yaml file with my refresh tokens and other important info.

I'm trying to deploy my flask app to render.com and I'm unsure what to do with the yaml files since I want to keep my tokens secure. Any advice?

So far I've tried adding my tokens in an env file and reading them that way, but it looks like the google ads api needs the yaml file.

I want to get Leadinfo from my Lead ads using python and the facebook API.

I have tried this:

import json from facebook_business.api import FacebookAdsApi from facebook_business.adobjects.adaccount import AdAccount from facebook_business.adobjects.leadgenform import LeadgenForm from facebook_business.adobjects.lead import Lead access_token = '<my_token>' ad_account_id = 'act_my_ad_acount' FacebookAdsApi.init(access_token=access_token) ad_account = AdAccount(ad_account_id) ads = ad_account.get_ads(fields=[         Ad.Field.id,         Ad.Field.name,     ]) leads = [] for ad in ads:   lead = ad.get_leads(fields=[     Lead.Field.id,     Lead.Field.field_data     ])   leads.extend(lead) print(leads) 

However it breaks due to this error:

There have been too many calls from this ad-account. Please wait a bit and try again. 

I understand that I should be doing some kind of batch call. But I found the documentation too hard do understand. It took me three days just get the code to list the ads in my account.

Could someone, please help me?

My end goal is to retrieve the information that the users sent on the leadforms ads, ei. their name, telephone, etc..

I am using python sdk facebook_bussiness for acceissng meta apis. So when we create ad we have to choose the call to action depending on the type of media uploaded if media is an image then CTA would be differnet and diffirent for Videos. My questions is there any endpoint/function in the sdk which return me the CTAs depending on the media type, so that I don't have to hardcore values in code.

I'm trying to extract a month worth of facebook posts into a csv file. I'm extracting from the 1st May until 30th May of 2024, but once my script done running, it only extracted posts from the 30th May 2024 (it started on this date) until 12th May 2024.

It doesn't extract from the 9th May until 1st May 2024.

Below is my code:

import pyfacebook from datetime import datetime, timedelta, timezone import json import pandas as pd import time # Initialize Facebook Graph API connection (replace placeholders with your actual credentials) graph = pyfacebook.GraphAPI(access_token='my-access-token', version='v20.0') # Define the page ID page_id = 'my-page-id' # date from_date = '2024-05-01' to_date = '2024-05-30' # Construct the API request URL (notice the parameters 'since', 'until' and 'fields') posts_url = f'/{page_id}/feed?fields=attachments,created_time&since={from_date}T08:00:00&until={to_date}T23:59:59' posts_data = graph._request(posts_url).json() while True:      if 'paging' in posts_data and 'next' in posts_data['paging']:         posts_url = posts_data['paging']['next']         posts_data = graph._request(posts_url).json()         time.sleep(2)  # Increased delay to be safe     else:         break  # No more pages to retrieve      df = pd.DataFrame(posts_data) df.to_csv('facebook_data.csv', index=False) 

Here is the result, it ends on the 9th May 2024 instead of continuing until 1st May 2024:

How do I extract the full month's worth of data?

I'm trying to create an display upload ad in Google Ads API v14, but I'm encountering issues. Specifically, I'm getting an "INVALID_ARGUMENT" error with the message "Mutates are not allowed for the requested resource."

def create_display_upload_ad_group_ad(client, customer_id, ad_group_id, ad_asset_resource_name):     """Creates a new HTML5 display upload ad and adds it to the given ad group.     Args:         client: An initialized Google Ads client.         customer_id: The Google Ads customer ID.         ad_group_id: The ID of the ad group to which the new ad will be added.         ad_asset_resource_name: The resource name of the media bundle containing             the HTML5 components.     """     # Get the AdGroupAdService client.     ad_group_ad_service = client.get_service("AdGroupAdService")     # Create an AdGroupAdOperation.     ad_group_ad_operation = client.get_type("AdGroupAdOperation")     # Configure the ad group ad fields.     ad_group_ad = ad_group_ad_operation.create     ad_group_ad.status = client.enums.AdGroupAdStatusEnum.PAUSED     ad_group_ad.ad_group = client.get_service("AdGroupService").ad_group_path(         customer_id, ad_group_id     )     # Configured the ad as a display upload ad.     display_upload_ad = ad_group_ad.ad     display_upload_ad.name = "Ad for HTML5"     display_upload_ad.final_urls.append("http://example.com/html5")     # Exactly one of the ad_data "oneof" fields must be included to specify the     # ad type. See: https://developers.google.com/google-ads/api/reference/rpc/latest/Ad for     # the full list of available types. By setting a "display_upload_ad"     # subfield it sets that as the "oneof" field for the Ad.     display_upload_ad.display_upload_ad.media_bundle.asset = (         ad_asset_resource_name     )     display_upload_ad.display_upload_ad.display_upload_product_type = (         client.enums.DisplayUploadProductTypeEnum.HTML5_UPLOAD_AD     )     # Add the ad group ad to the client account and display the resulting     # ad's resource name.     mutate_ad_group_ads_response = ad_group_ad_service.mutate_ad_group_ads(         customer_id=customer_id, operations=[ad_group_ad_operation]     )     print(         "Created new ad group ad with resource name "         f"'{mutate_ad_group_ads_response.results[0].resource_name}'."     ) 

Error Message:

Request with ID "xKDxTq8R83Lu7ElSsLBwYw" failed with status "INVALID_ARGUMENT" and includes the following errors: Error with message "Mutates are not allowed for the requested resource.".         On field: operations         On field: create         On field: ad 

file was uploaded and returned the path Uploaded file with resource name 'customers/12344/assets/12234' and I entered it in the function's ad_asset_resource_name
i try to v14

Despite these checks, I'm still encountering the same error. Any insights or suggestions on what might be going wrong would be greatly appreciated.