【发布时间】:2021-02-25 00:22:04
【问题描述】:
我正在尝试从 Google Ads API 获取广告系列的报告。 但是,尝试使用 MCC 获取数据时出错:
代码:
import sys,os
from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException
def get_data(client, customer_id):
ga_service = client.get_service("GoogleAdsService", version="v6")
query = """
SELECT
campaign.name,
campaign.status,
segments.device,
metrics.impressions,
metrics.clicks,
metrics.ctr,
metrics.average_cpc,
metrics.cost_micros
FROM campaign
WHERE segments.date DURING LAST_30_DAYS
"""
# 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(
row
)
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__":
# get client object with oauth2
credentials = {'developer_token': "xxxxxxxxxxxxxxx",
'refresh_token': "xxxxxxxxxxxxxxxx",
'client_id': "xxxxxxxxxxxxxxx",
'client_secret': "xxxxxxxxxxxxxxxx"
}
google_ads_client = GoogleAdsClient.load_from_dict(credentials)
get_data(google_ads_client, 'xxxxxxxxx')
Wehn 使用 MCC 客户端 ID 运行代码:
get_data(google_ads_client, 'MANAGER(MCC)_CLIENT_ID')
我遇到了 Error_1:
发出的请求:ClientCustomerId:xxxxxxxxx,主机:googleads.googleapis.com:443,方法:/google.ads.googleads.v6.services.GoogleAdsService/SearchStream,RequestId:xxxxxxxxxx,IsFault:True,FaultMessage:指标不能请求经理帐户。要检索指标,请针对经理帐号下的每个客户帐号发出单独的请求。
我认为,解决方案是为帐户本身设置不同的 ClientCustomerId,而不是 MCC。 所以我这样做了,并使用直接帐户的客户端 ID 再次运行代码,然后又遇到了另一个错误:
Wehn 使用 Account 客户端 ID 运行代码:
get_data(google_ads_client, 'ACCOUNT_CLIENT_ID')
我遇到了 Error_2:
发出的请求:ClientCustomerId:xxxxxxx,主机:googleads.googleapis.com:443,方法:/google.ads.googleads.v6.services.GoogleAdsService/SearchStream,RequestId:xxxxxxxxxx,IsFault:True,FaultMessage:用户没有' t 有权访问客户。注意:如果您正在访问客户客户,则必须在“login-customer-id”标头中设置经理的客户 ID。见https://developers.google.com/google-ads/api/docs/concepts/call-structure#cid
该错误本质上是要插入经理的客户 ID,我已经这样做并得到了 error_1 (!)。
我在这里错过了什么?
【问题讨论】:
标签: python google-ads-api