Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
987 views
in Technique[技术] by (71.8m points)

php - Google Adsense Services Account

I have the problem with Google Adsense API. I used Services Account to authen, and this is my code:

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("YYYYYY@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);

And then I got this error messege:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/adsense/v1.3/reports?startDate=2013-01-01&endDate=2013-03-03&metric=earnings&dimension=date: (403) User does not have an AdSense account.' in /opt/lampp/htdocs/googleads/google-api-php-client/io/Google_REST.php:66 Stack trace: #0 /opt/lampp/htdocs/googleads/google-api-php-client/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /opt/lampp/htdocs/googleads/google-api-php-client/service/Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /opt/lampp/htdocs/googleads/google-api-php-client/contrib/Google_AdSenseService.php(849): Google_ServiceResource->__call('generate', Array) #3 /opt/lampp/htdocs/googleads/index.php(28): Google_ReportsServiceResource->generate('2013-01-01', '2013-03-03', Array) #4 {main} thrown in /opt/lampp/htdocs/googleads/google-api-php-client/io/Google_REST.php on line 66

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately at the moment AdSense API doesn't support Service Account, so AdSense data is invisible for API (that's why you getting that error).

Perhaps the best solution is to use "Web application" login: https://developers.google.com/adsense/management/getting_started#auth


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...