Google 广告 API 查询返回 _StreamingResponseIterator
Posted
技术标签:
【中文标题】Google 广告 API 查询返回 _StreamingResponseIterator【英文标题】:Google ads API Query returns _StreamingResponseIterator 【发布时间】:2021-03-19 06:32:01 【问题描述】:我正在尝试为我的 Google 广告获取报告并将其显示在仪表板上以便于查看和监控。
我已经完成了对我的帐户进行身份验证的整个过程。我得到了所有需要的钥匙。 一切正常,直到查询运行。请参阅下面从Google ads API examples复制的代码
#!/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 example illustrates how to get all campaigns.
To add campaigns, run add_campaigns.py.
"""
import argparse
import sys
from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException
def main(client, customer_id):
ga_service = client.get_service("GoogleAdsService", version="v6")
query = """
SELECT campaign.id, campaign.name
FROM campaign
ORDER BY campaign.id"""
# Issues a search request using streaming.
response = ga_service.search_stream(customer_id, query=query)
try:
for batch in response:
for row in batch.results:
print(
f"Campaign with ID row.campaign.id and name "
f'"row.campaign.name" was found.'
)
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)
if __name__ == "__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
google_ads_client = GoogleAdsClient.load_from_storage()
parser = argparse.ArgumentParser(
description="Lists all campaigns for specified customer."
)
# The following argument(s) should be provided to run the example.
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
args = parser.parse_args()
main(google_ads_client, args.customer_id)
没有任何东西打印到我的控制台。当我打印出print(response)
我得到<google.api_core.grpc_helpers._StreamingResponseIterator object at 0x7fb7dcaaf3d0>
我没有收到任何错误,没有任何回溯。这是我的控制台的样子(隐藏 customer_id):
【问题讨论】:
看起来一切正常。您的 Google 广告帐户是否可能没有广告系列? 尝试打印len(response)
并检查是否为0。
@RitwikG 是的,我公司的谷歌广告帐户有 50 多个广告系列。这就是为什么我觉得它无法打印任何东西很奇怪。
@RitwikG TypeError: '_StreamingResponseIterator' 类型的对象没有 len()
MCC 没有广告系列,只有帐户有。您需要传递实际的帐户 ID,而不是您的 MCC ID。
【参考方案1】:
它实际上是返回值,你可以使用它。
for batch in response:
for row in batch.results:
print(
f"Campaign with ID row.campaign.id and name "
f'"row.campaign.name" was found.'
)
【讨论】:
对我来说,它没有返回任何值。程序运行,没有打印任何内容。这意味着查询没有结果。然而,关联的帐户上有多个广告系列。以上是关于Google 广告 API 查询返回 _StreamingResponseIterator的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 Google Analytics API 获取 Google Ads 关键字和广告内容
如何通过 Google Ads API 检索给定广告系列的受众