#!/usr/bin/perl -w # # Copyright 2006 Google Inc. All rights reserved. use SOAP::Lite; # Demo of using the Report service to generate AFC aggregate report. # 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 Report service connection $reportWsdlUrl = "http://www.google.com/api/adsense/v2/ReportService?WSDL"; my $reportService = SOAP::Lite->service($reportWsdlUrl); # Uncomment this line to display the XML request/response. #$reportService->on_debug( sub { print @_ }); # Disable autotyping. $reportService->autotype(0); # Register a fault handler. $reportService->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")); # Set up the date range. $dateRange = { "startDate" => "2005-11-11T08:00:00.000Z", "endDate" => "2006-02-02T08:00:00.000Z", "isPredefined" => "false", "predefinedDateRange" => "AllTime" }; # Set up the report. $afcReport = { "dateRange" => $dateRange, "outputFormat" => "CSV_Excel", "unit" => "Page" }; # Generate the report. We need to explicity specify the data type # because there are several different types of Report objects. $reportResult = $reportService->generateReport( SOAP::Data->name("report") ->type("ns4:AFCAggregateReport") ->value($afcReport) ->uri($namespace) ->prefix("ns4"), @headers ); print "Columns: " . $reportResult->{"columnCount"} . "\n"; print "Rows: " . $reportResult->{"rowCount"} . "\n"; print "Data: " . $reportResult->{"data"} . "\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); }