Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How to directly get the image_url saved in Google when uploading an image asset?

31 views
Skip to first unread message

xiaochuan li

unread,
Apr 18, 2025, 3:20:51 AMApr 18
to Google Ads API and AdWords API Forum
I am currently using `BatchJobService` to process the upload of `image_ad`. I encountered a problem when I `build_ad_group_ad_operation`. When I want to upload the image to Google and use Google's image connection, I found that I can't get the image link in Google: `https://tpc.googlesyndication.com/simgad/xxxxxxxxxxxxxxxxxxx`

```python
def build_ad_group_ad_operation(client, customer_id, ad_group_resource_name, ad_type, display_campaign_ad_group_ad_id):
    ad_group_ad_operation = client.get_type("AdGroupAdOperation")
    ad_group_ad = ad_group_ad_operation.create

    ad_group_ad.status = client.enums.AdGroupAdStatusEnum.ENABLED
    ad_group_ad.ad_group = ad_group_resource_name

    base_final_url = google_ads_account_info[str(customer_id)]['base_final_url']
    display_url = google_ads_account_info[str(customer_id)]['display_url']
    if ad_type == ads_models.DisplayCampaignPublishInfoAdTypeChoices.IMAGE_AD:
        display_campaign_ad_group_ad_obj = ads_models.DisplayCampaignAdGroupAdHistory.objects.filter(
            id=display_campaign_ad_group_ad_id
        ).first()
        material_obj = display_campaign_ad_group_ad_obj.creative.materials.first()
        origin_material_obj = material_obj.origin_material
        mime_type = origin_material_obj.mime_type
        pixel_width = int(origin_material_obj.resolution_width)
        pixel_height = int(origin_material_obj.resolution_height)
        image_url = origin_material_obj.file_url
        image_name = material_obj.name

        ad_group_ad.ad.final_urls.append(f'{base_final_url}/shares')

        ad_group_ad.ad.display_url = display_url
        ad_group_ad.ad.name = display_campaign_ad_group_ad_obj.creative.name
        ad_group_ad.ad.image_ad.pixel_width = pixel_width
        ad_group_ad.ad.image_ad.pixel_height = pixel_height

        # TODO: 方案一: 先把图片上传至google, 再使用google的图片链接, 但是暂时不知如何合并至batch中
        asset_resource_name, image_asset_image_url = create_ad_image_asset(
            client, customer_id, image_url, pixel_width, pixel_height, image_name
        )
        ad_group_ad.ad.image_ad.image_url = image_asset_image_url
        # TODO: 方案二: 使用 image_asset 信息, 可以直接放到batch中, 但是不知道最终效果使用的是什么图片  待验证
        # asset_resource_name, image_asset_image_url = create_ad_image_asset(
        #     client, customer_id, image_url, pixel_width, pixel_height, image_name
        # )
        # ad_group_ad.ad.image_ad.image_asset.asset = asset_resource_name

        if mime_type == 'image/jpeg':
            ad_group_ad.ad.image_ad.mime_type = client.enums.MimeTypeEnum.IMAGE_JPEG
        elif mime_type == 'image/png':
            ad_group_ad.ad.image_ad.mime_type = client.enums.MimeTypeEnum.IMAGE_PNG
        elif mime_type == 'image/gif':
            ad_group_ad.ad.image_ad.mime_type = client.enums.MimeTypeEnum.IMAGE_GIF
        else:
            ad_group_ad.ad.image_ad.mime_type = client.enums.MimeTypeEnum.UNSPECIFIED
    elif ad_type == ads_models.DisplayCampaignPublishInfoAdTypeChoices.IMAGE_AD_HTML5:
        ad_type_ = 'h5'
    elif ad_type == ads_models.DisplayCampaignPublishInfoAdTypeChoices.RESPONSIVE_DISPLAY_AD:
        ad_type_ = 'res'
    else:
        return []

    return ad_group_ad_operation

```

```python
def create_ad_image_asset(client, customer_id, image_url, width_pixels, height_pixels, image_name):
    response = requests.get(image_url)
    image_content = response.content
    mime_type = response.headers['Content-Type']

    asset_service = client.get_service("AssetService")
    asset_operation = client.get_type("AssetOperation")

    asset = asset_operation.create
    asset.type_ = client.enums.AssetTypeEnum.IMAGE
    asset.image_asset.data = image_content
    asset.image_asset.file_size = len(image_content)
    if mime_type == 'image/jpeg':
        asset.image_asset.mime_type = client.enums.MimeTypeEnum.IMAGE_JPEG
    elif mime_type == 'image/png':
        asset.image_asset.mime_type = client.enums.MimeTypeEnum.IMAGE_PNG
    elif mime_type == 'image/gif':
        asset.image_asset.mime_type = client.enums.MimeTypeEnum.IMAGE_GIF
    else:
        asset.image_asset.mime_type = client.enums.MimeTypeEnum.UNSPECIFIED
    asset.image_asset.full_size.width_pixels = width_pixels
    asset.image_asset.full_size.height_pixels = height_pixels
    asset.name = image_name

    mutate_asset_response = asset_service.mutate_assets(
        customer_id=str(customer_id), operations=[asset_operation]
    )
    print(f'mutate_asset_response: {mutate_asset_response}')
    asset_resource_name = mutate_asset_response.results[0].resource_name
    image_asset_image_url = mutate_asset_response.results[0].asset.image_asset.full_size.url

    return asset_resource_name, image_asset_image_url

```

The function `create_ad_image_asset` can only return `asset_resource_name`.
```python console
mutate_asset_response: results {
resource_name: "customers/11111111/assets/225688809944"
}
```
What I need is `asset.image_asset.full_size.url` and apply it to `ad_group_ad.ad.image_ad.image_url`.
How should I implement this function?

In addition, inserting a synchronization task into BatchJobService is a very irrational approach, but I don't know how to effectively upload images in batchjob and get `asset.image_asset.full_size.url`. Can it use a temporary id? If there is any good solution, please tell me. Thank you.

Google Ads API Forum Advisor

unread,
Apr 18, 2025, 7:21:14 AMApr 18
to clixia...@gmail.com, adwor...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads API support team.​

Based on the information you provided, I understand that you want to upload an image asset through Google Ads API. You can use this guide ​ which explains how to upload an image. You mentioned that you came across a problem when you tried to implement BatchJobService, kindly provide us with the complete API logs (request and response with request-id and request header) generated at your end to better assist you.

If you are using a client library and haven't enabled the logging yet, I would request you to enable logging for the specific client library that you are using. You can refer to the guides Java, .Net, PHP, Python, Ruby or Perl to enable logging at your end. For REST interface requests, you can enable logging via the curl command by using the -i flag.

Thanks,
 
Google Logo Google Ads API Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5
[2025-04-18 11:20:33Z GMT] This message is in relation to case "ref:!00D1U01174p.!500Ht01qUHiZ:ref" (ADR-00300404)



Reply all
Reply to author
Forward
0 new messages