Sorry for that the code above is the error of add_display_upload_ad.py,
and the code below is my code of add_smart_display_ads.py:
---
#!/usr/bin/env python
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This adds a smart display campaign, an ad group, and a responsive
display ad.
More information about Smart Display campaigns can be found at:
https://support.google.com/google-ads/answer/7020281
IMPORTANT: The AssetService requires you to reuse what you've uploaded
previously. Therefore, you cannot create an image asset with the exactly
same
bytes. In case you want to run this example more than once, note down the
created assets' resource names and specify them as command-line arguments
for
marketing and square marketing images.
Alternatively, you can modify the image URLs' constants directly to use
other
images. You can find image specifications in the pydoc for
ResponsiveDisplayAdInfo in common/ad_type_infos_pb2.py.
"""
import argparse
import datetime
import requests
import sys
from uuid import uuid4
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
_DATE_FORMAT = "%Y%m%d"
_MARKETING_IMAGE_URL = "https://gaagl.page.link/Eit5;
_MARKETING_IMAGE_WIDTH = 600
_MARKETING_IMAGE_HEIGHT = 315
_SQUARE_MARKETING_IMAGE_URL = "https://gaagl.page.link/bjYi;
_SQUARE_MARKETING_IMAGE_SIZE = 512
def main(
client,
customer_id,
marketing_image_asset_id=None,
square_marketing_image_asset_id=None,
):
budget_resource_name = _create_budget(client, customer_id)
print(f'Created budget with resource name "{budget_resource_name}".')
campaign_resource_name = _create_smart_display_campaign(
client, customer_id, budget_resource_name
)
print(
"Created smart display campaign with resource name "
f'"{campaign_resource_name}".'
)
ad_group_resource_name = _create_ad_group(
client, customer_id, campaign_resource_name
)
print(f'Created ad group with resource name "{ad_group_resource_name}"')
if marketing_image_asset_id:
print(
"Using existing marketing image asset with ID "
f'"{marketing_image_asset_id}".'
)
else:
marketing_image_asset_id = _upload_image_asset(
client,
customer_id,
_MARKETING_IMAGE_URL,
_MARKETING_IMAGE_WIDTH,
_MARKETING_IMAGE_HEIGHT,
"Marketing Image",
)
print(
"Created marketing image asset with ID "
f'"{marketing_image_asset_id}".'
)
if square_marketing_image_asset_id:
print(
"Using existing square marketing image asset with ID "
f'"{square_marketing_image_asset_id}".'
)
else:
square_marketing_image_asset_id = _upload_image_asset(
client,
customer_id,
_SQUARE_MARKETING_IMAGE_URL,
_SQUARE_MARKETING_IMAGE_SIZE,
_SQUARE_MARKETING_IMAGE_SIZE,
"Square Marketing Image",
)
print(
"Created square marketing image asset with ID "
f'"{square_marketing_image_asset_id}".'
)
responsive_display_ad_resource_name = _create_responsive_display_ad(
client,
customer_id,
ad_group_resource_name,
marketing_image_asset_id,
square_marketing_image_asset_id,
)
print(
"Created responsive display ad with resource name "
f'"{responsive_display_ad_resource_name}".'
)
print(
"Created responsive display ad with resource name "
f'"{responsive_display_ad_resource_name}".'
)
# [START add_smart_display_ad]
def _create_budget(client, customer_id):
campaign_budget_operation = client.get_type("CampaignBudgetOperation")
campaign_budget = campaign_budget_operation.create
campaign_budget.name = f"Interplanetary Cruise Budget #{uuid4()}"
campaign_budget.delivery_method = (
client.enums.BudgetDeliveryMethodEnum.STANDARD
)
campaign_budget.amount_micros = 50
campaign_budget_service = client.get_service("CampaignBudgetService")
try:
campaign_budget_response = (
campaign_budget_service.mutate_campaign_budgets(
customer_id=customer_id, operations=[campaign_budget_operation]
)
)
except GoogleAdsException as ex:
_handle_googleads_exception(ex)
return campaign_budget_response.results[0].resource_name
# [END add_smart_display_ad]
# [START add_smart_display_ad_1]
def _create_smart_display_campaign(client, customer_id,
budget_resource_name):
campaign_service = client.get_service("CampaignService")
campaign_operation = client.get_type("CampaignOperation")
campaign = campaign_operation.create
campaign.name = f"Smart Display Campaign #{uuid4()}"
advertising_channel_type_enum =