#!/usr/bin/perl -w # # Copyright 2006 Google Inc. All rights reserved. use SOAP::Lite; # Demo of using the Account service and the Channel service # to add a new URL channel. # SOAP Headers $developerEmail = 'REPLACE WITH DEVELOPER\'S EMAIL'; $developerPassword = 'REPLACE WITH DEVELOPER\'S PASSWORD'; $clientId = 'ca-pub-REPLACE WITH CLIENT 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 Channel service connection $channelServiceWsdlUrl = "http://www.google.com/api/adsense/v2/ChannelService?WSDL"; my $channelService = SOAP::Lite->service($channelServiceWsdlUrl); # Uncomment these lines to display the XML request/response. #$accountService->on_debug( sub { print @_ } ); #$channelService->on_debug( sub { print @_ }); # Disable autotyping. $accountService->autotype(0); $channelService->autotype(0); # Register a fault handler. $accountService->on_fault(\&faulthandler); $channelService->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"}; # Request a new channel for monitoring the URL http://www.example.com. # The value of id is ignored in the request. $channelServiceData = { "id" => "0", "name" => "http://www.example.com", "status" => "Active", "type" => "Url", "synServiceId" => $synServiceId }; $channelServiceResult = $channelService->createChannel($channelServiceData, @headers); print "Created a new " . $channelServiceResult->{"type"} . " channel with ID " . $channelServiceResult->{"id"} . " for " . $channelServiceResult->{"name"} . "\n"; ### 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); }