#!/usr/bin/python2.2
#
# Copyright 2006, Google Inc. All Rights Reserved.
#####################################################################
# Sample code to add a generate ad code snippet through Adsense API #
#####################################################################
import SOAPpy
server = "https://www.google.com"
dev_email = "REPLACE WITH DEVELOPER EMAIL"
dev_password = "REPLACE WITH DEVELOPER PASSWORD"
client_id = "REPLACE WITH CLIENT ID"
# Set headers
headers = SOAPpy.Types.headerType()
headers.developer_email = dev_email
headers.developer_password = dev_password
headers.client_id = client_id
# Set service connection
service = SOAPpy.SOAPProxy(
server + "/api/adsense/v2/AccountService",
header=headers)
# To view xml request/response set service.config.debug = 1
service.config.debug = 0
syn_service_type = SOAPpy.Types.untypedType("ContentAds")
# Get the Syndication ID
# Might throw a soap exception on user input or error upon trying to connect
response = service.getSyndicationService(syn_service_type)
syn_id = response["id"]
print "syn id :", syn_id
print
# Set another service connetion
service = SOAPpy.SOAPProxy(
server + "/api/adsense/v2/AdSenseForContentService",
header=headers)
service.config.debug = 0
# Set up the ad style
ad_style_template = """
%s
%s
%s
%s
%s
%s"""
ad_style = ad_style_template % ('demoStyle',' #0000FF', '#FFFFFF',
'#FF0000', '#00FF00', '#FFFF00')
ad_style = SOAPpy.Types.untypedType(ad_style)
ad_style._setAttr(
'xmlns:impl',
'http://www.google.com/api/adsense/v2')
ad_style._setAttr('xsi:type', 'impl:AdStyle')
# Set up other parameters
syn_service_id = SOAPpy.Types.untypedType(syn_id)
ad_unit_type = SOAPpy.Types.untypedType("TextOnly")
layout = SOAPpy.Types.untypedType("728x90")
alternate = SOAPpy.Types.untypedType("#FFFFFF")
is_framed_page = SOAPpy.Types.untypedType("false")
# Generate ad code, might throw a soap exception
# on user input or error upon trying to connect
response = service.generateAdCode(syn_service_id, ad_style, ad_unit_type,
layout, alternate, is_framed_page)
print "Ad code snippet :"
print response