适用于Python的Google Ads API:'_Rendezvous'对象没有属性'request_id'

时间:2019-02-07 16:53:15

标签: python google-api google-ads-api

我一直在尝试使用Python客户端库为Google Ads API复制以下代码示例。首先,我完成了身份验证过程,并使用google-ads.yamldeveloper_tokenclient_idclient_secret和{{1 }}。我要复制的示例代码如下,您也可以在下面的GitHub repository上查看。

refresh_token

我尝试以几种不同的方式在命令提示符下运行以上代码,如下所示:

login_customer_id

我什至在声明google_ads_client变量后注释掉了这些行,只是手动提供了客户ID

# Copyright 2018 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.
"""

from __future__ import absolute_import

import argparse
import six
import sys

import google.ads.google_ads.client


_DEFAULT_PAGE_SIZE = 1000


def main(client, customer_id, page_size):
    ga_service = client.get_service('GoogleAdsService')

    query = ('SELECT campaign.id, campaign.name FROM campaign '
             'ORDER BY campaign.id')

    results = ga_service.search(customer_id, query=query,page_size=page_size)

    try:
        for row in results:
            print('Campaign with ID %d and name "%s" was found.'
                  % (row.campaign.id.value, row.campaign.name.value))
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % 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 = (google.ads.google_ads.client.GoogleAdsClient
                         .load_from_storage('google-ads.yaml'))

    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=six.text_type,
                        required=True, help='The Google Ads customer ID.')
    args = parser.parse_args()

main(google_ads_client, args.customer_id, _DEFAULT_PAGE_SIZE)

并以这种方式运行脚本。对于所有这些情况,我仍然遇到相同的错误

> python get_campaigns.py --customer_id 'xxx-xxx-xxxx'
> python get_campaign.py --customer_id 'xxxxxxxxxx'

任何帮助,将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

尝试一下。

python get_campaigns.py --customer_id "123456789"

不带引号

答案 1 :(得分:0)

您实际上不需要定义或请求页面大小。您没有提供所需的令牌,但没有提供。

考虑到您的查询仅是请求广告系列,I'd suggest you use this version without the paging或令牌需求(新的Google Ads API中不需要分页,除非您想实际实现分页​​,而我不相信您会这样做)。

该版本将为您提供与第一个完全相同的结果,而无需分页。