Fatal Error in Adsense example in google-api-php-client

455 views
Skip to first unread message

Raúl Mellado

unread,
Feb 29, 2012, 1:52:19 AM2/29/12
to adsen...@googlegroups.com
   Hi everybody (specially to Silvano who kindly answers all of our questions),

   I am trying to develop a Wordpress plugin (in PHP) that will show, in the backend, earnings for a current blog (filtering per custom channels) using Management API. I am very new to Google Apis and OAuth, so I am a bit lost :-)

   The first thing I did was to download the package api-php-client (current version 0.4.9) from http://code.google.com/p/google-api-php-client/downloads/list.

   I first tried the tasks example and made it work, creating a project in Google APIs section, well done for me!  :-)

   Now I tried the Adsense example; I created a new project in the Google APIs, updated the example php code with my data (first difference I noticed was that there is this additional code: $this->apiClient->setDeveloperKey('YOUR_DEVELOPER_KEY_HERE');, could you add some light on where I can get my developer key?).

   When I upload the code to my server and load /examples/adsense/index.php, I get the message:

Fatal error: Call to a member function execute() on a non-object in /public_html/gapis/examples/adsense/AdSenseAuth.php on line 131 

   Now I check this problem, and it comes from here:

  public function authenticate($user) {
    $this->user = $user;
    $dbh = new PDO('sqlite:examples.sqlite');
    var_dump($dbh); //added by me

   For some reason, $dbh object is empty (added the var_dump myself to test).

   Do you have any idea about what could be happening here?


   And my second question here (well, this is for later on, when I can make the example code work), is this: as I would like each user to install this wp plugin in their own self-hosted WordPress installation, I have seen that I need to enter a redirect uri/JavaScript origin. Does this mean each user needs to add a new url for each blog they have? Or is there a better way to do this, to make it domain independent and make it easier for the user to install? (and last question for now -promised!- could I use the "offline" mode to gather data in a passive mode while the user is not logged in?).

   Thanks for your patience answering my newbie questions!


   Raul

Silvano Luciani

unread,
Feb 29, 2012, 5:15:19 AM2/29/12
to AdSense API Forum
Hello Raul,

thanks :D

You can find the DeveloperKey in the Google API Console, in the tab
'API Access':
https://code.google.com/apis/console/

At the end of the API Access page there is a box named Simple API
Access, API key is what you are looking for.
The DeveloperKey is meant to be used for request that need to be
signed but don't need authorization; for the AdSense Management API,
every request needs to be authorized, so the Key is not enough to send
requests.

About the error, you need to create the database 'example.sqlite',
looks like you didn't, the docs of the class 'AdSenseAuth' explain
what you need to do:
http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/adsense/AdSenseAuth.php#29

The answer for the redirect URI question is yes, every user will have
to create a project in the Google API Console, create a client id for
web application, set their redirect URI and Javascript Origins.

Last reply: yes, the offline access is meant exactly for that use
case. If you use the Google API PHP Client Library, is enabled by
default:
http://code.google.com/p/google-api-php-client/source/browse/trunk/src/auth/apiOAuth2.php#36

Cheers,
Silvano

--
Silvano Luciani | Developer Programs Engineer

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London
SW1W 9TQ
Registered in England Number: 3977902

On Feb 29, 6:52 am, Raúl Mellado <raulmell...@gmail.com> wrote:
>    Hi everybody (specially to Silvano who kindly answers all of our
> questions),
>
>    I am trying to develop a Wordpress plugin (in PHP) that will show, in
> the backend, earnings for a current blog (filtering per custom channels)
> using Management API. I am very new to Google Apis and OAuth, so I am a bit
> lost :-)
>
>    The first thing I did was to download the package api-php-client
> (current version 0.4.9) fromhttp://code.google.com/p/google-api-php-client/downloads/list.
>
>    I first tried the tasks example and made it work, creating a project in
> Google APIs section, well done for me!  :-)
>
>    Now I tried the Adsense example; I created a new project in the Google
> APIs, updated the example php code with my data (first difference I noticed
> was that there is this additional
> code: $this->apiClient->setDeveloperKey('YOUR_DEVELOPER_KEY_HERE');, could
> you add some light on where I can get my developer key?).
>
>    When I upload the code to my server and
> load /examples/adsense/index.php, I get the message:
>
> *Fatal error*: Call to a member function execute() on a non-object in *
> /public_html/gapis/examples/adsense/AdSenseAuth.php* on line *131*

Raúl Mellado

unread,
Mar 1, 2012, 1:55:44 AM3/1/12
to adsen...@googlegroups.com
   Hi Silvano,

   my apologies for having "doubted" your code for a second :-) I created the sqlite db and it worked like a charm! You made my day :-)

   One final question: I want to make my WordPress plugin noobie-friendly, so I want the users to do as little as possible to start using it. Could you please confirm this is the simplest way for a user to get my plugin up and running in, let's say, Blog A and Blog B?

   1. User goes to Google APIs site, activates Adsense API using his Google account.
   2. User creates a new Project (let's call it "WP Adsense API Stats")
   3. Within that project, user creates a client ID for blog A, entering redirect URL + javascript origins with URL for Blog A.
   4. User enters Client ID + Client secret + Developer key (q1: is this last one needed?) in the Wordpress Backend
   5. User authenticates Blog A, using oAuth against Google APIs (q2: is this authentication needed only once per url?)
   6. User does steps 3, 4 and 5 again for Blog B
   7. No more interaction from user side, my plugin can then collect stats without the user having to mess with APIs, authentication, etc? (again, I want to make as simple for the user as possible)
   8. Users thank me for a great and simple to use plugin. I thank Silvano for his great help. Silvano thanks whoever invented beer in the first place. Raul invites Silvano to some beers.

   q3: Would this be the way to go? No shortcuts/even simpler ways for the user here?

   q4: do I need to store user + token in Wordpress db (maybe this is simpler here than using a sqlite database)? My guess here is that this time is going to change on each session, right?

   Once more, thanks a lot for your great help, it's been really useful!


   Raul

PS. To put you in perspective: I have found a couple of plugins that do something similar to what I am trying to accomplish (get adsense stats from WordPress backend), but they were coded before the API was public, so here is how they work:

   1. User enters Google username/password, which are then stored unencrypted (*sigh*) in Wordpress db
   2. WP plugin logins on Adsense using stored user/pass and scrapes page, showing earnings in WP backend.

   I am trying to do it in a more elegant (and safe) way, using the available Management API :-)

Silvano Luciani

unread,
Mar 1, 2012, 6:29:46 AM3/1/12
to AdSense API Forum
Hello Raúl,

you could use the same Client ID for both blogs, adding redirect URIs
and Javascript origins for both.
When authenticating from Blog A you would set in the PHP client Blog A
redirect URI, Blog B redirect URI when authenticating from Blog B.
If you use the same Client ID, the authentication is valid on both
blogs. If you use offline_access, the user will have to authorize the
plugin only the first time it's being used, then the token will be
automatically refreshed when needed.

If you want to store a token, you can use anything that will allow you
to save strings. When you request offline access, you receive a json
object that contains access_token (the value you need to use to
authorize requests), expires_in (the validity of the token in seconds)
and a refresh_token(that you use to retrieve another token when it has
expired). So basically you can save the access_token for expires_in
seconds since you have received it, then you'll need to refresh it
using the refresh token. The refresh token will be the same unless the
user revokes the authorization to the application.

If you want to know more about the flow, you can read our docs on
Using OAuth 2.0 for Web Server Applications:
https://developers.google.com/accounts/docs/OAuth2WebServer

Cheers,
Silvano

--
Silvano Luciani | Developer Programs Engineer

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London
SW1W 9TQ
Registered in England Number: 3977902

> >http://code.google.com/p/google-api-php-client/source/browse/trunk/ex...
>
> > The answer for the redirect URI question is yes, every user will have
> > to create a project in the Google API Console, create a client id for
> > web application, set their redirect URI and Javascript Origins.
>
> > Last reply: yes, the offline access is meant exactly for that use
> > case. If you use the Google API PHP Client Library, is enabled by
> > default:
>
> >http://code.google.com/p/google-api-php-client/source/browse/trunk/sr...

Raúl Mellado

unread,
Mar 23, 2012, 5:03:56 AM3/23/12
to adsen...@googlegroups.com
   Thanks a lot for your really useful help, Silvano. Everything on my side is working now thanks to your great support!

Jose Alcérreca (AdSense API Team)

unread,
Mar 8, 2013, 9:35:19 AM3/8/13
to adsen...@googlegroups.com
Hi Dominique,

Reading previous comments by Silvano, he points to:


* Assumes the presence of a sqlite database called './examples.sqlite'
 * containing a table called 'auth' composed of two VARCHAR(255) fields called
 * 'user' and 'token'.

sqlite is really easy to use, just search for "sqlite tutorial" on the Web.

Let me know if you need more advice.

Cheers,
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


On Thursday, 7 March 2013 18:06:44 UTC, Dominique HUGO wrote:
Hi Silvano,
 
I know this thread is a bit old but I'm trying to use your code and I get the error about example.sqlite; as you said, we need to create this database but can you give me some guidelines to do it about the structure, I don't find it
 
sorry to bother you with this & thanks in advance,
Domi

Dominique HUGO

unread,
Mar 11, 2013, 12:21:39 PM3/11/13
to adsen...@googlegroups.com
Hi Jose,
 
first of all, sorry for the delay of my/this reponse.
 
I don't know why I haven't seen this structure in the comment Silvano...... !!
 
So, thanks a lot for your response, my table is now created.
 
Sincerly,
Domi.

Jose Alcérreca (AdSense API Team)

unread,
Apr 30, 2013, 5:57:34 AM4/30/13
to adsen...@googlegroups.com
Ciao Matteo,

That doesn't seem to be a configuration problem. Are you sure the program is accessing SQLite correctly? Can you query it from the command line or a GUI?

Cheers,
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


On Monday, 29 April 2013 16:00:22 UTC+1, Matteo Giampaolo wrote:
HI,

I've just followed all (hope) instruction so:
1) install Sqlite
2) create table auth with e fileds (user and token) on ./esamples.sqlite file

unfortunately I receive the same fatal error on AdsenseAuth.php (Fatal error: Call to a member function execute() on a non-object in /webroot/report/google-api-php-client/examples/adsense/AdSenseAuth.php on line 130)

Are there any other configuration/Action I've to perform to be able to use the index.php file (/google-api-php-client/examples/adsense)?

Thx
Matteo

Matteo Giampaolo

unread,
Apr 30, 2013, 1:15:06 PM4/30/13
to adsen...@googlegroups.com
Yes, command line is OK (insert, select, delete ecc.)
Is there any other check?
Could be some security issue (ie grant access to file or dir)?

Thx

Jose Alcérreca (AdSense API Team)

unread,
May 1, 2013, 9:51:32 AM5/1/13
to adsen...@googlegroups.com
Hi Matteo,

esamples.sqlite should be examples.sqlite.

Cheers,
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902

Matteo Giampaolo

unread,
May 3, 2013, 10:11:25 AM5/3/13
to adsen...@googlegroups.com
Sorry but esamples was a typo....

let's start from my starting problem
Is there a way to access data (directly as Json, report CSV ecc.) using a PHP application running from a server on crontab?
The issue is that I've several Adsense account and I'd like to collect data on a DB

Thx
Matteo

Jose Alcérreca (AdSense API Team)

unread,
May 7, 2013, 6:54:39 AM5/7/13
to adsen...@googlegroups.com
Hi Matteo,

You can, that's what the AdSense Management API is for. 


Cheers!
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Reply all
Reply to author
Forward
0 new messages