首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google ads API查询返回_StreamingResponseIterator

Google ads API查询返回_StreamingResponseIterator
EN

Stack Overflow用户
提问于 2021-03-19 14:32:01
回答 1查看 388关注 0票数 0

我正在尝试获取我的google广告的报告,并将它们显示在仪表板上,以便更容易查看和监控。

我已经完成了验证我的帐户的整个过程。我拿到了所有需要的钥匙。在查询运行之前,一切都会正常进行。请参阅从Google ads API examples复制的以下代码

代码语言:javascript
复制
#!/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):

EN

回答 1

Stack Overflow用户

发布于 2021-05-09 11:10:06

它实际上返回了值,你可以使用它。

代码语言:javascript
复制
 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.'
        )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66703509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档