python - 如何使用 Google API 和 Python 从 Google Ads 广告系列中删除位置条件
问题描述
我可以使用以下相同的代码将位置条件添加到活动中,方法是替换campaign_criterion = campaign_criterion_operation.remove
为campaign_criterion = campaign_criterion_operation.create
但是,我无法使用以下代码根据位置 ID 删除位置。我浏览了文档,但找不到任何有用的东西。任何帮助将不胜感激。
def main(client, customer_id, campaign_id, location_id):
campaign_criterion_service = client.get_service("CampaignCriterionService")
operations = [
_create_location_op(client, customer_id, campaign_id, location_id)
]
print(operations)
campaign_criterion_response = (
campaign_criterion_service.mutate_campaign_criteria(
customer_id=customer_id, operations=operations
)
)
for result in campaign_criterion_response.results:
print(f'Added campaign criterion "{result.resource_name}".')
def _create_location_op(client, customer_id, campaign_id, location_id):
campaign_service = client.get_service("CampaignService")
geo_target_constant_service = client.get_service("GeoTargetConstantService")
# campaign criterion.
campaign_criterion_operation = client.get_type("CampaignCriterionOperation")
campaign_criterion = campaign_criterion_operation.remove
campaign_criterion.campaign = campaign_service.campaign_path(
customer_id, campaign_id
)
# Besides using location_id, you can also search by location names from
# GeoTargetConstantService.suggest_geo_target_constants() and directly
# apply GeoTargetConstant.resource_name here. An example can be found
# in get_geo_target_constant_by_names.py.
campaign_criterion.location.geo_target_constant = (
geo_target_constant_service.geo_target_constant_path(location_id)
)
return campaign_criterion_operation
def otherMain():
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
googleads_client = GoogleAdsClient.load_from_storage(path='XXX')
campaign_id = XX
customer_id = XX
location_id = XX
try:
main(googleads_client, customer_id, campaign_id , location_id)
except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)
我得到的错误是:
campaign_criterion.campaign = campaign_service.campaign_path(
AttributeError: 'str' object has no attribute 'campaign'
解决方案
推荐阅读
- javascript - 可选链接“全部向右”?
- php - 未定义的属性:Laravel 单元测试中的 Mockery_3_App_Repositories_ArticleRepositoryInterface::$id
- cat - cat 只打印一个文件描述符
- java - 将唯一对象实例添加到 ArrayList 的方法不起作用
- python - 如何使用 django rest api 上传多个文件?
- python - 循环迭代程序正确性示例 - 循环不变量和程序终止
- html - 在导航栏社交图标中对齐项目不会居中
- python - opencv 图像的自定义名称(复杂名称)
- python - 使用反斜杠 python 返回 os.getcwd 问题
- python - TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“结果”