如何让Google AdWords API Python客户端库运行?

时间:2011-03-08 17:25:08

标签: python google-adwords

是否有可用于使用Python adwords客户端的最新教程,或者是否有人知道取消以下内容?我是否犯了一些配置错误,或者Python 2.6不支持?

在Windows上我安装了:

  • Python 2.6
  • setuptools的-0.6c11.win32-py2.6.exe
  • 的PyXML-0.8.4
  • SOAPpy的-0.12.4
  • ZSI-2.0-RC3
  • adwords_api_python_13.2.0

我已向AdWords配置程序提供了MCC帐户电子邮件地址,密码,空白客户端电子邮件和ID,并为开发人员令牌提供了附加“+ USD”的同一电子邮件地址。

现在,如果我使用SOAPpy作为SOAP库,我会得到一个关于意外关键字参数timeout的异常,该参数由SOAPpy中的一个函数提供给SOAPpy中的另一个函数。

如果我使用ZSI作为SOAP库(我认为是首选),我会得到以下异常:

Traceback (most recent call last):
  File "tutorial.py", line 36, in <module>
    campaigns = campaign_service.Mutate(operations)[0]
  File "c:\Python26\lib\site-packages\adspygoogle\adwords\CampaignService.py", l
ine 112, in Mutate
    'Campaign', self._loc, request)
  File "c:\Python26\lib\site-packages\adspygoogle\adwords\AdWordsWebService.py",
 line 256, in CallMethod
    self.__ManageSoap(buf, start_time, stop_time, error)
  File "c:\Python26\lib\site-packages\adspygoogle\adwords\AdWordsWebService.py",
 line 130, in __ManageSoap
    raise Error(e)
adspygoogle.common.Errors.Error:
Traceback (most recent call last):
  File "c:\Python26\lib\site-packages\adspygoogle\common\WebService.py", line 20
8, in CallMethod
    eval('service.%s(request)' % method_name))
  File "<string>", line 1, in <module>
  File "c:\Python26\lib\site-packages\adspygoogle\adwords\zsi\v201008\CampaignSe
rvice_services.py", line 47, in mutateCampaign
    self.binding.Send(None, None, request, soapaction="", **kw)
  File "build\bdist.win32\egg\ZSI\client.py", line 267, in Send
    self.SendSOAPData(soapdata, url, soapaction, **kw)
  File "build\bdist.win32\egg\ZSI\client.py", line 301, in SendSOAPData
    self.h.send(soapdata)
  File "c:\Python26\lib\site-packages\adspygoogle\common\zsi\HttpsConnectionHand
ler.py", line 93, in send
    httplib.HTTPSConnection.endheaders(self)
  File "c:\Python26\lib\httplib.py", line 892, in endheaders
    self._send_output()
  File "c:\Python26\lib\httplib.py", line 764, in _send_output
    self.send(msg)
  File "c:\Python26\lib\site-packages\adspygoogle\common\zsi\HttpsConnectionHand
ler.py", line 93, in send
    httplib.HTTPSConnection.endheaders(self)
  File "c:\Python26\lib\httplib.py", line 890, in endheaders
    raise CannotSendHeader()
CannotSendHeader [RAW DATA: _________________________________ Tue Mar 08 16:54:4
0 2011 REQUEST:

后面跟着一些XML,我已修改它以删除识别数据:

<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xm
lns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ZSI="http://www.z
olera.com/schemas/ZSI/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header><RequestHeader xmlns
="https://adwords.google.com/api/adwords/cm/v201008"><authToken>XYZ</authToken>
<userAgent>AwApi-Python-13.2.0|XYZ</userAgent><developerTok
en>XYZ@gmail.com+USD</developerToken></RequestHeader></SOAP-ENV:Header><SO
AP-ENV:Body xmlns:ns1="https://adwords.google.com/api/adwords/cm/v201008"><ns1:m
utate xmlns:ns1="https://adwords.google.com/api/adwords/cm/v201008"><ns1:operati
ons><ns1:operator>ADD</ns1:operator><ns1:operand><ns1:name>Interplanetary Cruise
 #201138165440485000</ns1:name><ns1:status>PAUSED</ns1:status><ns1:endDate>20120
101</ns1:endDate><ns1:budget xsi:type="ns1:Budget"><ns1:period>DAILY</ns1:period
><ns1:amount xsi:type="ns1:Money"><ns1:microAmount>50000000</ns1:microAmount></n
s1:amount><ns1:deliveryMethod>STANDARD</ns1:deliveryMethod></ns1:budget><ns1:bid
dingStrategy xsi:type="ns1:ManualCPC"></ns1:biddingStrategy></ns1:operand></ns1:
operations></ns1:mutate></SOAP-ENV:Body></SOAP-ENV:Envelope>

我的代码如下:

from adspygoogle.adwords.AdWordsClient import AdWordsClient
from adspygoogle.common import Utils

client = AdWordsClient(path = '../../installers/adwords_api_python_13.2.0.tar/adwords_api_python_13.2.0')

campaign_service = client.GetCampaignService('https://adwords-sandbox.google.com', 'v201008')

operations = [{
    'operator': 'ADD',
    'operand': {
        'name': 'Interplanetary Cruise #%s' % Utils.GetUniqueName(),
        'status': 'PAUSED',
        'biddingStrategy': {
            'type': 'ManualCPC'
        },
        'endDate': '20120101',
        'budget': {
            'period': 'DAILY',
            'amount': {
                'microAmount': '50000000'
            },
            'deliveryMethod': 'STANDARD'
        }
    }
}]

campaigns = campaign_service.Mutate(operations)[0]

# Display results.
for campaign in campaigns['value']:
  print ('Campaign with name \'%s\' and id \'%s\' was added.'
         % (campaign['name'], campaign['id']))

print
print ('Usage: %s units, %s operations' % (client.GetUnits(),
                                           client.GetOperations()))

请注意,http://code.google.com/apis/adwords/docs/tutorial.html上的教程代码甚至不起作用,在当前版本的客户端中没有aw_api.Client这样的东西。但以上内容大部分是从教程中复制的。

客户端提供的示例会导致相同的错误。

如果当前的客户端库被破坏,我可以尝试使用旧的客户端库,但如果它有任何区别,我将需要一个支持TargetingIdeaService的客户端库。

[编辑:哦,抱歉,我忘了说我实际上编辑了AdWords客户端代码。在Utils.GetDataFromCsvFile中,我从构造传递给.replace(':','|')的文件网址的行中删除了urllib.urlopen

这使我怀疑我的Python版本可能不受支持,因为Windows Python 2.6似乎无法打开网址file:///c|/foo/bar/baz/]

1 个答案:

答案 0 :(得分:2)

您是否尝试过ZSI-2.0?此问题报告与http://code.google.com/p/google-api-adwords-python-lib/issues/detail?id=141类似。如果您继续看到此问题,请将您的信息附加到错误报告中。

至于教程代码过时,好抓!我马上就修好了。请跟踪http://code.google.com/p/google-api-adwords-python-lib/issues/detail?id=152