Google Adsense服务帐户 [英] Google Adsense Services Account

查看:207
本文介绍了Google Adsense服务帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Google Adsense API有问题。我使用服务帐户进行身份验证,这是我的代码:

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:

致命错误:消息为调用GET https://www.googleapis.com/adsense/v1.3/reports?startDate=2013-01- 01& endDate = 2013-03-03& metric = earnings& dimension = date :(403)用户没有AdSense帐户。位于/ opt / lampp / htdocs / googleads / google-api-php- client / io / Google_REST.php:66堆栈跟踪:#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):Goog le_ServiceResource-> __ call('generate',Array)#3 /opt/lampp/htdocs/googleads/index.php(28):Google_ReportsServiceResource-> generate('2013-01-01','2013-03-03',数组)在第66行上抛出#4 {main}到/opt/lampp/htdocs/googleads/google-api-php-client/io/Google_REST.php中

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


推荐答案

服务帐户在AdSense Management API中不可用。

Service accounts are not available in the AdSense Management API.

如果要使用API在没有用户干预的情况下,您可以使用 Web应用程序或已安装的应用程序方案。用户授予您对应用程序的访问权限后,您就可以开始请求访问令牌。

If you want to use the API without user intervention, you can use the "Web application" or "Installed application" scenarios. Once the user grants access to your app, you can start requesting access tokens.

查看以下PHP示例: https://code.google.com/p/google-api-php-client / source / browse /#svn%2Ftrunk%2Fexamples%2Fadsense

Check out the PHP samples: https://code.google.com/p/google-api-php-client/source/browse/#svn%2Ftrunk%2Fexamples%2Fadsense

这篇关于Google Adsense服务帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆