'Google ads API Query returns _StreamingResponseIterator
I'm trying to get reports for my google ads and display them on a dashboard for easier viewing and monitoring.
I've gone through the whole process to authenticate my account. I got all the keys needed. Everything works up until the query is run. See code below copied from 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)
Nothing gets printed to my console. When I print out print(response)
i get <google.api_core.grpc_helpers._StreamingResponseIterator object at 0x7fb7dcaaf3d0>
I get no errors, tracebacks nothing. This is what my console looks like (hiding customer_id):
Solution 1:[1]
It actually returns the value and you can use it.
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.'
)
Solution 2:[2]
You have access to a managerial account that doesn`t have actual campaigns. TO fix it you should get access to customer accounts. More information you can find here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mohsen shafiey |
Solution 2 | Eugene |