如何让 Google AdWords API Python 客户端库正常工作?
是否有可用的最新教程可用于使用 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
我已向 MCC 提供了 AdWords 配置程序帐户电子邮件地址、密码、空白客户电子邮件地址和 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>[email protected]+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()))
客户端提供的示例会导致相同的错误。
如果当前的客户端库完全损坏,我可以尝试使用较旧的客户端库,但如果它有任何区别,我将需要一个支持 TargetingIdeaService 的客户端库。
[编辑:哦,抱歉,我忘了说我实际上编辑了 AdWords 客户端代码。在 Utils.GetDataFromCsvFile 中,我从构造传递给 urllib.urlopen
的文件 url 的行中删除了 .replace(':','|')
。
这让我怀疑我的 Python 版本可能不受支持,因为 Windows Python 2.6 似乎无法打开 url file:///c|/foo/bar/baz/
]
Is there an up-to-date tutorial available for using the Python adwords client, or failing that does anyone know enough to unpick the following? Have I made some configuration error, or perhaps is Python 2.6 unsupported?
On Windows I've installed:
- 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
I've supplied the AdWords config program with the MCC account email address, its password, blank client email and id, and for the developer token the same email address with "+USD" appended.
Now, if I use SOAPpy as the SOAP library, I get an exception about an unexpected keyword parameter timeout
, which has been supplied by one function in SOAPpy to another function in SOAPpy.
If I use ZSI as the SOAP library (which I believe is preferred), I get the following exception:
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:
followed by some XML, which I've modified to remove identifying data:
<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>[email protected]+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>
My code is below:
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()))
Note that the tutorial code at http://code.google.com/apis/adwords/docs/tutorial.html doesn't even slightly work, there's no such thing as aw_api.Client
in the current version of the client. But the above is mostly copied from the tutorial.
The examples provided with the client result in the same error.
I could try an older client library if the current one is simply broken, although if it makes any difference, I will need one that supports TargetingIdeaService.
[Edit: oh, sorry, I forgot to say that I actually edited the AdWords client code. In Utils.GetDataFromCsvFile, I removed .replace(':','|')
from the line that constructs the file url passed to urllib.urlopen
.
This is what makes me suspect that my version of Python might not be supported, since Windows Python 2.6 didn't seem able to open the url file:///c|/foo/bar/baz/
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你试过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。
Have you tried with ZSI-2.0? This issue report looks similar to http://code.google.com/p/google-api-adwords-python-lib/issues/detail?id=141. Please append your info to the bug report, if you continue to see this issue.
As for tutorial code being out of date, good catch! I'll have it fixed shortly. Please track at http://code.google.com/p/google-api-adwords-python-lib/issues/detail?id=152.