#!/usr/bin/perl -w # # Copyright 2005 Google Inc. All rights reserved. use SOAP::Lite; # Demo of using the Account service and the AdSenseForContent # service to generate a javascript ad code snippet. # SOAP Headers $developerEmail = 'REPLACE WITH DEVELOPER\'S EMAIL'; $developerPassword = 'REPLACE WITH DEVELOPER\'S PASSWORD'; $clientId = 'REPLACE WITH CLIENT\'S ID'; # The namespace used for API headers. my $namespace = "http://www.google.com/api/adsense/v2"; # Set up the Account service connection $accountWsdlUrl = "http://www.google.com/api/adsense/v2/AccountService?WSDL"; my $accountService = SOAP::Lite->service($accountWsdlUrl); # Set up the AdSenseForContent connection $adSenseForContentWsdlUrl = "http://www.google.com/api/adsense/v2/AdSenseForContentService?WSDL"; my $adSenseForContentService = SOAP::Lite->service($adSenseForContentWsdlUrl); # Uncomment this line to display the XML request/response. #$accountService->on_debug( sub { print @_ } ); #$adSenseForContentService->on_debug( sub { print @_ }); # Disable autotyping. $accountService->autotype(0); $adSenseForContentService->autotype(0); # Register a fault handler. $accountService->on_fault(\&faulthandler); $adSenseForContentService->on_fault(\&faulthandler); my @headers = (SOAP::Header->name("developer_email")->value($developerEmail)->uri($namespace)->prefix("impl"), SOAP::Header->name("developer_password")->value($developerPassword)->uri($namespace)->prefix("impl"), SOAP::Header->name("client_id")->value($clientId)->uri($namespace)->prefix("impl")); # Get the user's AdSense for Content syndication service ID $synServiceType = "ContentAds"; $synServiceData = $accountService->getSyndicationService($synServiceType, @headers); $synServiceId = $synServiceData->{"id"}; # we need to create a style for the code we will generate; we could # use a style we previously saved with saveAdStyle, but here we assume # we don't have any such styles $adStyle = { "name" => "demoStyle", "borderColor" => "#0000FF", "backgroundColor" => "#FFFFFF", "titleColor" => "#FF0000", "textColor" => "#00FF00", "urlColor" => "#FFFF00", }; # create the other parameters to generateAdCode $adUnitType = "TextOnly"; $layout = "728x90"; $alternate = "#FFFFFF"; $isFramedPage = "false"; $channelName = undef; my $codeSnippet = $adSenseForContentService->generateAdCode($synServiceId, $adStyle, $adUnitType, $layout, $alternate, $isFramedPage, $channelName, @headers); print "The code snippet to put on the page:" . "\n"; print $codeSnippet; ### Helper functions sub faulthandler { my ($soap, $res) = @_; my $errorMessage = "SOAP Fault: " . "Error Code " . $res->faultdetail->{"code"} . ". " . $res->faultdetail->{"message"}; if (defined $res->faultdetail->{"trigger"}) { $errorMessage .= " \"" . $res->faultdetail->{"trigger"} . "\" "; } if (defined $res->faultdetail->{"triggerDetails"}) { $errorMessage .= $res->faultdetail->{"triggerDetails"}; } die($errorMessage); }