Google 广告 API - 如何使用 MCC(经理帐户)获取数据

Google Ads API - How to fetch data with MCC (manager account)

我正在尝试从 Google 广告 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 运行 帐户客户 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: 用户无权访问客户。注意:如果您正在访问客户客户,则必须在 'login-customer-id' header 中设置经理的客户 ID。参见 https://developers.google.com/google-ads/api/docs/concepts/call-structure#cid

该错误基本上是说要插入经理的客户 ID,我已经这样做并得到了 error_1 (!)。

我在这里错过了什么?

已解决:

该函数应使用帐户客户端 ID:

启动
get_data(google_ads_client, 'ACCOUNT_CLIENT_ID')

并且,应将键值对添加到凭据字典(MCC 客户端 ID)中:

    credentials = {'developer_token': "xxxxxxxxxxxxxxx",
        'refresh_token': "xxxxxxxxxxxxxxxx",
        'client_id': "xxxxxxxxxxxxxxx",
        'client_secret': "xxxxxxxxxxxxxxxx",
        // Add this line below
        'login_customer_id': 'mcc_client_id'
                   }