seif elsherif
seif elsherif

Reputation: 25

Google adsense api (403) User does not have an AdSense account

it have been 4 hours trying to troubleshoot this problem,it always return with (403) User does not have an AdSense account , I am trying to get report of my adsense on my website, Ps:it's server to server connection

    require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
    require_once '../src/Google/Client.php';
    require_once '../src/Google/Service/AdSense.php';

    $client_email = '[email protected]';
    $private_key = file_get_contents('adsense-xxxxx.p12');
    $scopes = array('https://www.googleapis.com/auth/adsense.readonly');
    $credentials = new Google_Auth_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key,
    'notasecret'
    );
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_AdSense($client);

Error Log: PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/adsense/v1.4/accounts?maxResults=50: (403) User does not have an AdSense account.' in /var/www/geturl/ad/src/Google/Http/REST.php:110

#0 /var/www/geturl/ad/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))
#1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request))
#2 /var/www/geturl/ad/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array)
#3 /var/www/geturl/ad/src/Google/Http/REST.php(46): Google_Task_Runner->run()
#4 /var/www/geturl/ad/src/Google/Client.php(593): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#5 /var/www/geturl/ad/src/Google/Service/Resource.php(240): Google_Client->execute(Object(Google_Http_Request))
#6 /var/www/geturl/ad/src/Google/Service/AdSense.php(1106): Google_Service_Resource->call('list', Array, 'Google_Servic in /var/www/geturl/ad/src/Google/Http/REST.php on line 110

Upvotes: 0

Views: 1419

Answers (1)

peixotorms
peixotorms

Reputation: 1283

Did you wait for 24 hours after enabling the api?

Try testing the account to make sure that it works outside of code. Use the APIs explorer:

https://developers.google.com/adsense/management/v1.4/reference/accounts/list#try-it

Authenticate and try to fetch accounts list. If that works, make sure you are connecting to the API using that account credentials.

Edit:

Perhaps one of these can help:

https://github.com/googleads/googleads-adsense-examples/tree/master/php-clientlib-1.x/v1.x
https://developers.google.com/adsense/management/libraries
http://www.binarytides.com/php-get-adsense-earnings-and-reports/

Upvotes: 0

Related Questions