【发布时间】:2019-02-07 16:53:15
【问题描述】:
我一直在尝试使用 Python 客户端库为 Google Ads API 复制以下代码示例。让我先说一下,我完成了身份验证过程并使用developer_token、client_id、client_secret、refresh_token 和login_customer_id 配置了google-ads.yaml 文件。我希望复制的示例代码如下,您也可以在以下GitHub repository 中查看。
# 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'
我什至在声明 google_ads_client 变量后注释掉了这些行,并手动提供了客户 ID
main(google_ads_client, 'xxxxxxxxxx', _DEFAULT_PAGE_SIZE)
main(google_ads_client, 'xxx-xxx-xxxx', _DEFAULT_PAGE_SIZE)
并以这种方式运行脚本。对于所有这些情况,我仍然遇到同样的错误
AttributeError: '_Rendezvous' object has no attribute 'request_id'
任何帮助,将不胜感激。谢谢你。
【问题讨论】:
标签: python google-api google-ads-api