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 = 500000 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 = client.enums.AdvertisingChannelTypeEnum campaign.advertising_channel_type = advertising_channel_type_enum.DISPLAY advertising_channel_sub_type_enum = ( client.enums.AdvertisingChannelSubTypeEnum ) # Smart Display campaign requires the advertising_channel_sub_type as # "DISPLAY_SMART_CAMPAIGN". campaign.advertising_channel_sub_type = ( advertising_channel_sub_type_enum.DISPLAY_SMART_CAMPAIGN ) campaign_status_enum = client.enums.CampaignStatusEnum campaign.status = campaign_status_enum.PAUSED # Smart Display campaign requires the TargetCpa bidding strategy. campaign.target_cpa.target_cpa_micros = 5000000 campaign.campaign_budget = budget_resource_name # Optional: Set the start and end date. start_date = datetime.date.today() + datetime.timedelta(days=1) campaign.start_date = start_date.strftime(_DATE_FORMAT) end_date = start_date + datetime.timedelta(days=365) campaign.end_date = end_date.strftime(_DATE_FORMAT) try: campaign_response = campaign_service.mutate_campaigns( customer_id=customer_id, operations=[campaign_operation] ) except GoogleAdsException as ex: _handle_googleads_exception(ex) return campaign_response.results[0].resource_name # [END add_smart_display_ad_1] # [START add_smart_display_ad_4] def _create_ad_group(client, customer_id, campaign_resource_name): ad_group_service = client.get_service("AdGroupService") ad_group_operation = client.get_type("AdGroupOperation") ad_group = ad_group_operation.create ad_group.name = f"Earth to Mars Cruises #{uuid4()}" ad_group_status_enum = client.enums.AdGroupStatusEnum ad_group.status = ad_group_status_enum.PAUSED ad_group.campaign = campaign_resource_name try: ad_group_response = ad_group_service.mutate_ad_groups( customer_id=customer_id, operations=[ad_group_operation] ) except GoogleAdsException as ex: _handle_googleads_exception(ex) return ad_group_response.results[0].resource_name # [END add_smart_display_ad_4] # [START add_smart_display_ad_3] def _upload_image_asset( client, customer_id, image_url, image_width, image_height, image_name ): # Download image from URL image_content = requests.get(image_url).content asset_operation = client.get_type("AssetOperation") asset = asset_operation.create # Optional: Provide a unique friendly name to identify your asset. If you # specify the name field, then both the asset name and the image being # uploaded should be unique, and should not match another ACTIVE asset in # this customer account. # asset.name = f'Jupiter Trip #{uuid4()}' asset_type_enum = client.enums.AssetTypeEnum asset.type_ = asset_type_enum.IMAGE asset.name = image_name image_asset = asset.image_asset image_asset.data = image_content image_asset.file_size = len(image_content) image_asset.mime_type = client.enums.MimeTypeEnum.IMAGE_JPEG image_asset.full_size.width_pixels = image_width image_asset.full_size.height_pixels = image_height image_asset.full_size.url = image_url asset_service = client.get_service("AssetService") content_type_enum = client.enums.ResponseContentTypeEnum request = client.get_type("MutateAssetsRequest") request.customer_id = customer_id request.operations = [asset_operation] # Setting this parameter tells the API to return the Asset object in the # response, allowing us to easily retrieve its ID. request.response_content_type = content_type_enum.MUTABLE_RESOURCE try: mutate_asset_response = asset_service.mutate_assets(request=request) return mutate_asset_response.results[0].asset.id except GoogleAdsException as ex: _handle_googleads_exception(ex) # [END add_smart_display_ad_3] # [START add_smart_display_ad_2] def _create_responsive_display_ad( client, customer_id, ad_group_resource_name, marketing_image_asset_id, square_marketing_image_asset_id, ): ad_group_ad_service = client.get_service("AdGroupAdService") ad_group_ad_operation = client.get_type("AdGroupAdOperation") ad_group_ad = ad_group_ad_operation.create ad_group_ad.ad_group = ad_group_resource_name ad_group_ad.status = client.enums.AdGroupAdStatusEnum.PAUSED ad = ad_group_ad.ad ad.final_urls.append("https://www.example.com") responsive_display_ad = ad.responsive_display_ad # Add a headline headline = client.get_type("AdTextAsset") headline.text = "Travel" responsive_display_ad.headlines.append(headline) # Add the long headline responsive_display_ad.long_headline.text = "Travel the World" # Add a description description = client.get_type("AdTextAsset") description.text = "Take to the air!" responsive_display_ad.descriptions.append(description) # Add the business name responsive_display_ad.business_name = "Google" asset_service = client.get_service("AssetService") # Add a marketing image marketing_image = client.get_type("AdImageAsset") marketing_image.asset = asset_service.asset_path( customer_id, marketing_image_asset_id ) responsive_display_ad.marketing_images.append(marketing_image) # Add a square marketing image square_marketing_image = client.get_type("AdImageAsset") square_marketing_image.asset = asset_service.asset_path( customer_id, square_marketing_image_asset_id ) responsive_display_ad.square_marketing_images.append(square_marketing_image) # Add the call to action responsive_display_ad.call_to_action_text = "Shop Now" # Add the price prefix responsive_display_ad.price_prefix = "as low as" # Add the promo text responsive_display_ad.promo_text = "Free shipping!" try: ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads( customer_id=customer_id, operations=[ad_group_ad_operation] ) except GoogleAdsException as ex: _handle_googleads_exception(ex) return ad_group_ad_response.results[0].resource_name # [END add_smart_display_ad_2] def _handle_googleads_exception(exception): print( f'Request with ID "{exception.request_id}" failed with status ' f'"{exception.error.code().name}" and includes the following errors:' ) for error in exception.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) if __name__ == "__main__": # GoogleAdsClient will read the google-ads.yaml configuration file in the # home directory if none is specified. googleads_client = GoogleAdsClient.load_from_storage(version="v10", path="C:/Users/Chiao/startinvest/prj17/google_ads_tutorials-master/creds_example/googleads.yaml") parser = argparse.ArgumentParser( description=( "Creates a Smart Display campaign, and an ad group that " "are then used to create a responsive display ad." ) ) # The following argument(s) should be provided to run the example. parser.add_argument( "-c", "--customer_id", type=str, help="The Google Ads customer ID.", ) parser.add_argument( "-m", "--marketing_image_asset_id", type=str, help=("The ID for an image asset to be used as a marketing image."), ) parser.add_argument( "-s", "--square_marketing_image_asset_id", type=str, help=( "The resource name for an image asset to be used as a square " "marketing image." ), ) args = parser.parse_args() main( googleads_client, "7199745982", marketing_image_asset_id="40081329863", square_marketing_image_asset_id="40080135580", ) On Friday, May 27, 2022 at 10:34:56 AM UTC-7 Chiao Hsiao wrote: > Hello, > I am running the script of add_amart_display_ads.py, but I failed to do > the create operation as the following code: > > ------------------------------------------------------------------------------------------------------------------------------------------- > Created budget with resource name "customers/7199745982 <(719)%20974-5982> > /campaignBudgets/10971713488". > Request with ID "42zRfQLX_cfQvRh3hs_xLg" failed with status > "INVALID_ARGUMENT" and includes the following errors: > Error with message "Unauthorized CREATE operation in invoking a > service's mutate method.". > On field: operations > On field: create > On field: advertising_channel_sub_type > Error with message "Conversion tracking is not enabled for the > campaign for VBB transition.". > On field: operations > On field: create > On field: target_cpa > Request made: ClientCustomerId: 7199745982 <(719)%20974-5982>, Host: > googleads.googleapis.com, Method: > /google.ads.googleads.v10.services.CampaignService/MutateCampaigns, > RequestId: 42zRfQLX_cfQvRh3hs_xLg, IsFault: True, FaultMessage: > Unauthorized CREATE operation in invoking a service's mutate method. > > -------------------------------------------------------------------------------------------------------------------------------- > Note: I have the customer_Id, created in the test account, and all > credentials in googleads.yaml file, I can create another campaign such as > responsive search campaign successfully, so I think my customer_Id and all > credential in googleads.yaml file are correct, could you help me to solve > the problem? > > --------------------------------------------------------------------------------------------------------------------------------------------- > > The code below is my script: > [image: 螢幕擷取畫面 2022-05-27 103050.png] > #!/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. > """Adds a display upload ad to a given ad group. > To get ad groups, run get_ad_groups.py. > """ > > > import argparse > import sys > > import requests > > from google.ads.googleads.client import GoogleAdsClient > from google.ads.googleads.errors import GoogleAdsException > > > BUNDLE_URL = "https://gaagl.page.link/ib87" > > > def main(client, customer_id, ad_group_id): > """Adds a display upload ad to a 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. > """ > # There are several types of display upload ads. For this example, we will > # create an HTML5 upload ad, which requires a media bundle. > # This feature is only available to allowlisted accounts. > # See https://support.google.com/google-ads/answer/1722096 for more > details. > # The DisplayUploadProductType field lists the available display upload > types: > # > https://developers.google.com/google-ads/api/reference/rpc/latest/DisplayUploadAdInfo > > # Creates a new media bundle asset and returns the resource name. > ad_asset_resource_name = _create_media_bundle_asset(client, customer_id) > > # Creates a new display upload ad and associates it with the specified > # ad group. > _create_display_upload_ad_group_ad( > client, customer_id, ad_group_id, ad_asset_resource_name > ) > > > def _create_media_bundle_asset(client, customer_id): > """Creates a media bundle from the assets in a zip file. > The zip file contains the HTML5 components. > Args: > client: An initialized Google Ads client. > customer_id: The Google Ads customer ID for which the call is made. > Returns: > The string resource name of the newly uploaded media bundle. > """ > # Get the AssetService client. > asset_service = client.get_service("AssetService") > > # Construct an asset operation and populate its fields. > asset_operation = client.get_type("AssetOperation") > media_bundle_asset = asset_operation.create > media_bundle_asset.type_ = client.enums.AssetTypeEnum.MEDIA_BUNDLE > media_bundle_asset.name = "Ad Media Bundle" > # The HTML5 zip file contains all the HTML, CSS, and images needed for the > # HTML5 ad. For help on creating an HTML5 zip file, check out Google Web > # Designer (https://www.google.com/webdesigner/). > # Download the ZIP as bytes from the URL > media_bundle_asset.media_bundle_asset.data = requests.get( > BUNDLE_URL > ).content > > # Adds the asset to the client account. > mutate_asset_response = asset_service.mutate_assets( > customer_id=customer_id, operations=[asset_operation] > ) > > # Display and return the resulting resource name. > uploaded_asset_resource_name = mutate_asset_response.results[ > 0 > ].resource_name > print(f"Uploaded file with resource name > '{uploaded_asset_resource_name}'.") > > return uploaded_asset_resource_name > > > 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}'." > ) > > > if __name__ == "__main__": > # GoogleAdsClient will read the google-ads.yaml configuration file in the > # home directory if none is specified. > googleads_client = GoogleAdsClient.load_from_storage(version="v10", > > path="C:/Users/Chiao/startinvest/prj17/google_ads_tutorials-master/creds_example/googleads.yaml") > > try: > # main(googleads_client, args.customer_id, args.ad_group_id) > main(googleads_client, "7199745982 <(719)%20974-5982>", "136711598106") > 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) > > > -- -- =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ Also find us on our blog: https://googleadsdeveloper.blogspot.com/ =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ You received this message because you are subscribed to the Google Groups "AdWords API and Google Ads API Forum" group. To post to this group, send email to adwords-api@googlegroups.com To unsubscribe from this group, send email to adwords-api+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/adwords-api?hl=en --- You received this message because you are subscribed to the Google Groups "Google Ads API and AdWords API Forum" group. To unsubscribe from this group and stop receiving emails from it, send an email to adwords-api+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-api/98275324-aeb7-4f09-9553-28cf88741b12n%40googlegroups.com.