Authen by Service Account Error

185 views
Skip to first unread message

Stats VNG

unread,
Aug 30, 2013, 3:37:23 AM8/30/13
to adsen...@googlegroups.com
Hi all,
require_once dirname(__FILE__).'/google-api-php-client/Google_Client.php';

    require_once dirname(__FILE__).'/google-api-php-client/contrib/Google_AdSenseService.php';
    require_once dirname(__FILE__).'/google-api-php-client/contrib/Google_Oauth2Service.php';


    $SERVICE_ACCOUNT_PKCS12_FILE_PATH = dirname(__FILE__).'/keyfile.p12';

    // create client object and set app name
    $client = new Google_Client();
    $client->setApplicationName("XXXX"); // name of your app


    // set assertion credentials
    $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
    $auth = new Google_AssertionCredentials(
            "YYY...@developer.gserviceaccount.com", // email you added to GA
            array('https://www.googleapis.com/auth/adsense.readonly'),
            $key);

    $client->setAssertionCredentials($auth);
    $client->getAuth()->refreshTokenWithAssertion();
    $accessToken = $client->getAccessToken();
    // other settings
    $client->setClientId("XYZ.apps.googleusercontent.com");           // from API console
    $service = new Google_AdsenseService($client);
    $optParams = array('metric' => array('earnings'), 'dimension' => 'date');
      $data = $service -> reports -> generate('2013-01-01', '2013-03-03', $optParams);

But I got the error messege :
(403) User does not have an AdSense account
Please help me!!

galeksic

unread,
Aug 30, 2013, 5:25:50 AM8/30/13
to adsen...@googlegroups.com

Jose Alcérreca (AdSense API Team)

unread,
Aug 30, 2013, 5:38:54 AM8/30/13
to adsen...@googlegroups.com
Hi,

Service accounts don't work with the AdSense Management API. Please use installed application or web application. Once you have a refresh token there is no need of human intervention.

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,
Oct 22, 2013, 12:26:51 PM10/22/13
to adsen...@googlegroups.com
Hi Jose

but refresh token last "only"  3600 sec...isn't it?
If Yes, how can I manage a "refresh" without human interaction?

Thx

Jose Alcérreca (AdSense API Team)

unread,
Oct 22, 2013, 12:33:32 PM10/22/13
to adsen...@googlegroups.com
Hi Matteo,

Don't worry, the client libraries handle that for you. They will use your stored refresh token (that doesn't expire) to get a new access token (that expires in less than an hour).

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,
Oct 22, 2013, 12:54:00 PM10/22/13
to adsen...@googlegroups.com

Jose,

I'm still having problem with client library....
I though It could be possible managing in different way (example by storing authorization code and performing request like DFP API does...)

here response I get from index.php (/google-api-php-client/examples)

Runtime error: Could not json decode the token
#0 /dati/google-api-php-client/src/Google_Client.php(172): Google_OAuth2->setAccessToken(NULL)
#1 /dati/google-api-php-client/examples/adsense/AdSenseAuth.php(86): Google_Client->setAccessToken(true)
#2 /dati/google-api-php-client/examples/adsense/index.php(52): AdSenseAuth->authenticate('sample_user')

Sqlite seems could create DB (I find a  examples.sqlite file in the directory)

Is there a Deatailed guide on how to use/works example ?

Jose Alcérreca (AdSense API Team)

unread,
Oct 22, 2013, 1:08:14 PM10/22/13
to adsen...@googlegroups.com
If you want to get an access token manually, you can read this
Using a Refresh Token
As indicated in the previous section, a refresh token is obtained in offline scenarios during the first authorization code exchange. In these cases, your application may obtain a new access token by sending a refresh token to the Google OAuth 2.0 Authorization server.
To obtain a new access token this way, your application performs an HTTPs POST to https://accounts.google.com/o/oauth2/token. The request must include the following parameters...


The instructions to configure the PHP client library are in

To run the AdSense samples, you'll have to set up a sqlite database, but there is no guide on the installation:

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,
Oct 22, 2013, 1:33:40 PM10/22/13
to adsen...@googlegroups.com
I've read several time the PHP instruction but there's no issue (only dowload files....)
I've also tried creating DB (examples.sqlite and table auth) but then I get different errors

Runtime error: SQLSTATE[HY000]: General error: 26 file is encrypted or is not a database
#0 /dati/google-api-php-client/examples/adsense/AdSenseAuth.php(137): PDO->prepare('SELECT token FR...')
#1 /dati/google-api-php-client/examples/adsense/AdSenseAuth.php(75): AdSenseAuth->getToken(Object(PDO))

#2 /dati/google-api-php-client/examples/adsense/index.php(52): AdSenseAuth->authenticate('sample_user')

is there any other action I've to perform (other than download library and create sqlite DB)?

in the meanwhile I try to adrees adsense API with a "manually managed token"...

thx








galeksic

unread,
Oct 22, 2013, 3:00:45 PM10/22/13
to adsen...@googlegroups.com
tried creating DB (examples.sqlite and table auth) but then I get different errors
General error: 26 file is encrypted or is not a database

Did you created example.sqlite with lower SQLite version?

Would it be "safe" to rename that file and start auth again?

$dbh = new PDO('sqlite:examples.sqlite');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare('CREATE TABLE IF NOT EXISTS auth ' .
    '(user VARCHAR(255), token VARCHAR(255))');
$stmt->execute();


Or to create it "auto-manually" :)

<?php
/* create-example-sqlite.php */
try {
    $dbh = new PDO("sqlite:example.sqlite");
    $dbh = $dbh->prepare('CREATE TABLE IF NOT EXISTS auth ' .
        '(user VARCHAR(255), token VARCHAR(255))');
    $dbh->execute();
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

galeksic

unread,
Oct 22, 2013, 3:28:56 PM10/22/13
to adsen...@googlegroups.com
@Jose Alcérreca I'm not frequent poster on this forum, but if you allow - just one small suggestion / question :)

In Forum configuration, in "Information", on "Web view customization" page, there should be "Source code highlighting" option - could it be enabled?

"Code highlighting" could perhaps be convenient option for AdSense API coders - if that option exists on this forum, and if it is not disabled because of some reason, of course. :)

Jose Alcérreca (AdSense API Team)

unread,
Oct 23, 2013, 5:21:14 AM10/23/13
to adsen...@googlegroups.com
result = Jose.investigate()
print result

>>> Done!

Matteo, 

Looks like it's a version mismatch problem. What versions of PHP and SQLite are you using?

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

galeksic

unread,
Oct 23, 2013, 6:10:14 AM10/23/13
to adsen...@googlegroups.com
Oooops... infinite loop here. :)

Thank you!
Thank you!
Thank you!
<CTRL><C>
KeyboardInterrupt
>>> _

Matteo Giampaolo

unread,
Oct 23, 2013, 12:09:49 PM10/23/13
to adsen...@googlegroups.com
php 5.3.10
sqlite 2.8.17

Jose Alcérreca (AdSense API Team)

unread,
Oct 23, 2013, 12:29:59 PM10/23/13
to adsen...@googlegroups.com
Matteo,

After a quick serach, in the PHP documentation I found:

In PHP 5.1, the SQLite extension also provides a driver for SQLite 2 databases; while it is not technically a part of the PDO_SQLITE driver, it behaves similarly, so it is documented alongside it. The SQLite 2 driver for PDO is provided primarily to make it easier to import legacy SQLite 2 database files into an application that uses the faster, more efficient SQLite 3 driver. As a result, the SQLite 2 driver is not as feature-rich as the SQLite 3 driver.

Can you upgrade to SQLite 3?

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 Wednesday, 23 October 2013 17:09:49 UTC+1, Matteo Giampaolo wrote:
php 5.3.10
sqlite 2.8.17

Matteo Giampaolo

unread,
Oct 23, 2013, 1:24:46 PM10/23/13
to adsen...@googlegroups.com

upgraded to sqlite3, modified code (AdSenseAuth.php to use sqlite3 ---> $dbh = new PDO('sqlite3:examples.sqlite')
Still receive error
oRuntime error: Could not json decode the token

#0 /dati/google-api-php-client/src/Google_Client.php(172): Google_OAuth2->setAccessToken(NULL)
#1 /dati/google-api-php-client/examples/adsense/AdSenseAuth.php(86): Google_Client->setAccessToken(true)
#2 /dati/google-api-php-client/examples/adsense/index.php(52): AdSenseAuth->authenticate('sample_user')

BUT....Now I can enter in "examples.sample" DB (from outside php) and list an empty "Auth" table created by AdSenseAuth code...

Thx



Jose Alcérreca (AdSense API Team)

unread,
Oct 24, 2013, 12:13:44 PM10/24/13
to adsen...@googlegroups.com
Well, this problem is about your specific environment and it doesn't look related with the API or the client library. 

Have you installed the JSON PHP extension?

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


Message has been deleted

Matteo Giampaolo

unread,
Oct 25, 2013, 6:26:05 AM10/25/13
to adsen...@googlegroups.com
Jose,

thanks for all your support.
I succed in connect and use api by access token manually....

 
Reply all
Reply to author
Forward
0 new messages