How do I get the Google AdWords API Python Client Library to work?
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\CampaignS开发者_开发知识库ervice.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>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>
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/
]
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.
精彩评论