【发布时间】:2021-08-18 12:46:49
【问题描述】:
希望你们一切都好。我是使用 Google 广告 API 的新手。我必须检索有关关键字的信息,即有多少人搜索了某些关键字,有多少点击等等......所以我在 Google 广告上创建了一个经理帐户,并在该帐户下创建了客户帐户。在客户帐户中,我在关键字规划器下添加了关键字,我得到了上面提到的所有信息,但我想通过 python 中的 REST API 来获取它。
我拥有访问 API 所需的一切: (开发者令牌 login_customer_id 客户编号 客户秘密 刷新令牌)我已经在 .yaml 文件中给出了这些信息。我假设 login_customer_id 是经理帐户 ID。 下面是访问所有关键字信息的代码。这里我给出了我想要访问关键字信息的client_id。
导入参数解析 导入系统 从 google.ads.googleads.client 导入 GoogleAdsClient 从 google.ads.googleads.errors 导入 GoogleAdsException
def main(client, customer_id): ga_service = client.get_service("GoogleAdsService")
query = """
SELECT
campaign.id,
campaign.name,
ad_group.id,
ad_group.name,
ad_group_criterion.criterion_id,
ad_group_criterion.keyword.text,
ad_group_criterion.keyword.match_type,
metrics.impressions,
metrics.clicks,
metrics.cost_micros
FROM keyword_view WHERE segments.date DURING LAST_7_DAYS
AND campaign.advertising_channel_type = 'SEARCH'
AND ad_group.status = 'ENABLED'
AND ad_group_criterion.status IN ('ENABLED', 'PAUSED')
ORDER BY metrics.impressions DESC
LIMIT 50"""
# Issues a search request using streaming.
search_request = client.get_type("SearchGoogleAdsStreamRequest")
search_request.customer_id = customer_id
search_request.query = query
response = ga_service.search_stream(search_request)
for batch in response:
for row in batch.results:
campaign = row.campaign
ad_group = row.ad_group
criterion = row.ad_group_criterion
metrics = row.metrics
print(
f'Keyword text "{criterion.keyword.text}" with '
f'match type "{criterion.keyword.match_type.name}" '
f"and ID {criterion.criterion_id} in "
f'ad group "{ad_group.name}" '
f'with ID "{ad_group.id}" '
f'in campaign "{campaign.name}" '
f"with ID {campaign.id} "
f"had {metrics.impressions} impression(s), "
f"{metrics.clicks} click(s), and "
f"{metrics.cost_micros} cost (in micros) during "
"the last 7 days."
)
# [END get_keyword_stats]
如果 name == "main":
googleads_client=GoogleAdsClient.load_from_storage("C:\Users\AnoshpaBansari\PycharmProjects\GoogleAPI\src\creds\googleads.yaml")
parser = argparse.ArgumentParser(
description=("Retrieves a campaign's negative keywords.")
)
# 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()
try:
main(googleads_client, "----------")
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)
但是当我运行代码时,我收到了这个错误。我不知道我做错了什么..有人可以帮忙吗? enter image description here
【问题讨论】:
-
你找到解决办法了吗?
标签: google-ads-api rest