2016-02-13 107 views
0

已尝试解决此问题4个小时,它始终以(403)返回用户没有AdSense帐户,我正在尝试获取我的报告在我的网站的AdSense, PS:这是服务器到服务器的连接Google adsense api(403)用户没有AdSense帐户

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); 

错误日志: PHP致命错误:未捕获的异常“Google_Service_Exception”有消息“错误调用get https://www.googleapis.com/adsense/v1.4/accounts?maxResults=50:(403)用户不拥有AdSense帐户。'在/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 
+0

看起来你没有使用正确的API凭证。您是否已在您的帐户下启用api? – peixotorms

+0

是的,我已经在api控制台中启用了adsense mangment –

回答

0

你有没有等待24小时启用API后?

尝试测试帐户以确保它在代码之外工作。使用API​​浏览器:

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

验证并尝试获取帐户列表。 如果有效,请确保使用该帐户凭据连接到API。

编辑:

也许这些可以帮助:

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/ 
+0

已测试并返回200好了,我认为这是代码中的问题,或者帐户链接 –

+0

也许这可以帮助你(示例文件夹):https://github.com/googleads/ googleads-ADSENSE - 实例/树/主/ PHP-clientlib-1.x的/ 1.x版 – peixotorms