Gossamer Forum
Home : General : Perl Programming :

AdSense API

Quote Reply
AdSense API
Hi,

I'm trying to write a plugin, that will log into AdSense, with your details - and grab the code to show on your site. This is aimed to work with my AdSenseShare plugin - which doesn't seem to work fully when you use the new AdSense code they are giving you (with AdSenseChannel) .

The code I have it:
Code:
# ==================================================================
# Plugins::GetAdsenseCodeAPI - Auto Generated Program Module
#
# Plugins::GetAdsenseCodeAPI
# Author : Andy Newby
# Version : 1
# Updated : Tue Apr 8 04:03:36 2008
#
# ==================================================================
#

package Plugins::GetAdsenseCodeAPI;
# ==================================================================

use strict;
use GT::Base;
use GT::Plugins qw/STOP CONTINUE/;
use Links qw/:objects/;

# Inherit from base class for debug and error methods
@Plugins::GetAdsenseCodeAPI::ISA = qw(GT::Base);

# Your code begins here.


# PLUGIN HOOKS
# ===================================================================


sub do_signup {
# -----------------------------------------------------------------------------
# This subroutine will be called whenever the hook 'use_signup' is run. You
# should call $PLG->action(STOP) if you don't want the regular
# 'use_signup' code to run, otherwise the code will continue as normal.
#
my (@args) = @_;

# google_email google_pwd google_pub_id

print $IN->header;
print "HERE";

if ($IN->param('google_email') && $IN->param('google_pwd') && $IN->param('google_pub_id')) {
get_google_code($IN->param('google_email'),$IN->param('google_pwd'),$IN->param('google_pub_id'));
}

return @args;
}


sub get_google_code {

use SOAP::Lite;

# SOAP Headers
my $developerEmail = $_[0]; # 'REPLACE WITH DEVELOPER\'S EMAIL';
my $developerPassword = $_[1]; #'REPLACE WITH DEVELOPER\'S PASSWORD';
my $clientId = $_[2]; #'REPLACE WITH CLIENT\'S ID';

print qq~
Email: $developerEmail<br />
Pass: $developerPassword <br />
ClientID: $clientId
~;

# The namespace used for API headers.
my $namespace = "http://www.google.com/api/adsense/v2";

# Set up the Account service connection
my $accountWsdlUrl = "http://www.google.com/api/adsense/v2/AccountService?WSDL";
my $accountService = SOAP::Lite->service($accountWsdlUrl);

# Set up the AdSenseForContent connection
my $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
my $synServiceType = "ContentAds";
my $synServiceData = $accountService->getSyndicationService($synServiceType, @headers);
my $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
my $adStyle = {
"name" => "demoStyle",
"borderColor" => "#0000FF",
"backgroundColor" => "#FFFFFF",
"titleColor" => "#FF0000",
"textColor" => "#00FF00",
"urlColor" => "#FFFF00",
};

# create the other parameters to generateAdCode
my $adUnitType = "TextOnly";
my $layout = "728x90";
my $alternate = "#FFFFFF";
my $isFramedPage = "false";
my $channelName = undef;

my $codeSnippet = $adSenseForContentService->generateAdCode($synServiceId,
$adStyle, $adUnitType, $layout, $alternate, $isFramedPage, $channelName,
@headers);

print $IN->header;
print "GOOGEL CODE:" . $codeSnippet;
#return $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);
}



# Always end with a 1.
1;

Now, this is called fine - and I see what I would expect on the debug info:

Quote:
HERE Email: andy.newby@gmail.com
Pass: (my password - removed)
ClientID: pub-3171934375285541


..but I get this error:

Code:
A fatal error has occured:

SOAP Fault: Error Code 114. The 'developer_email' header does not represent an authorized developer. at /home/wwwjoin/public_html/cgi-bin/directory/admin/Plugins/GetAdsenseCodeAPI.pm line 143.

Please enable debugging in setup for more details.

Do you have to get something special done, for this to work? This code is taken from - http://code.google.com/...AdCodeSnippet.pl.txt (I had to edit it a bit, to add in "my" statements, so it would work with strict)

Any suggestions are much welcomed.

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] AdSense API In reply to
Still struggling with this one :(

I've now found out you have to signup to their Developer API system, here: http://services.google.com/...nquiry/api_info_form

Getting this error:

Quote:
SOAP Fault: Error Code 101. The request did not contain a header named 'developer_email'. at test2.cgi line 133.

I'm calling this script in SSH with:

perl test2.cgi --email='adsensedeveloper1@google.com' --pass='EDITEDOUT' --pubid='pub-3171934375285541'

test.cgi code:

Code:
#!/usr/bin/perl

# ==================================================================

use strict;

use Getopt::Long;

do_signup();

sub do_signup {
# -----------------------------------------------------------------------------
# This subroutine will be called whenever the hook 'use_signup' is run. You
# should call $PLG->action(STOP) if you don't want the regular
# 'use_signup' code to run, otherwise the code will continue as normal.
#
# google_email google_pwd google_pub_id



my ($email,$pass,$pubid);

GetOptions ('email=s' => \$email, 'pass=s' => \$pass, 'pubid=s' => \$pubid,);


if ($email && $pass && $pubid) {
get_google_code($email , $pass , $pubid);
} else {
print qq|Incorrect args|;
}

# if ($IN->param('google_email') && $IN->param('google_pwd') && $IN->param('google_pub_id')) {
# get_google_code($IN->param('google_email'),$IN->param('google_pwd'),$IN->param('google_pub_id'));
# }


}


sub get_google_code {

use SOAP::Lite;

# SOAP Headers
my $developerEmail = $_[0]; # 'REPLACE WITH DEVELOPER\'S EMAIL';
my $developerPassword = $_[1]; #'REPLACE WITH DEVELOPER\'S PASSWORD';
my $clientId = $_[2]; #'REPLACE WITH CLIENT\'S ID';

print qq~
Email: $developerEmail
Pass: $developerPassword
ClientID: $clientId
~;

# The namespace used for API headers.
my $namespace = "http://www.google.com/api/adsense/v2";

# Set up the Account service connection
my $accountWsdlUrl = "http://www.google.com/api/adsense/v2/AccountService?WSDL";
my $accountService = SOAP::Lite->service($accountWsdlUrl);

# Set up the AdSenseForContent connection
my $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
my $synServiceType = "ContentAds";
my $synServiceData = $accountService->getSyndicationService($synServiceType, @headers);
my $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
my $adStyle = {
"name" => "demoStyle",
"borderColor" => "#0000FF",
"backgroundColor" => "#FFFFFF",
"titleColor" => "#FF0000",
"textColor" => "#00FF00",
"urlColor" => "#FFFF00",
};

# create the other parameters to generateAdCode
my $adUnitType = "TextOnly";
my $layout = "728x90";
my $alternate = "#FFFFFF";
my $isFramedPage = "false";
my $channelName = undef;

my $codeSnippet = $adSenseForContentService->generateAdCode($synServiceId,
$adStyle, $adUnitType, $layout, $alternate, $isFramedPage, $channelName,
@headers);

print "GOOGEL CODE:" . $codeSnippet;
#return $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);
}



# Always end with a 1.
1;

Yet, the headers clearly show the developer_email bit does exist in the headers:

Code:
<SOAP-ENV:Header>
<impl:developer_email xmlns:impl="http://sandbox.google.com/api/adsense/v2">adsensedeveloper1@google.com</impl:developer_email>
<impl:developer_password xmlns:impl="http://sandbox.google.com/api/adsense/v2">MY_PASSWORD</impl:developer_password>
<impl:client_id xmlns:impl="http://sandbox.google.com/api/adsense/v2">pub-3171934375285541</impl:client_id>
</SOAP-ENV:Header>

Anyone got any ideas? This is driving me nuts Pirate

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] AdSense API In reply to
Eugh, damn it! Not sure if its gonna help me anyway :/


Quote:
Important: Please note that the live version of the AdSense API is only available to websites with over 100,000 daily page views across user pages. If your site qualifies and you're ready to start developing against the live service, proceed to the instructions at http://code.google.com/apis/adsense/gettingstarted.html.

Pirate

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!