[GoogleAnalytics API]エラー:(403) User does not have any Google Analytics account. が出るときの対処法

GoogleAnalytics APIを使用したプログラムで、APIをコールすると、”User does not have any Google Analytics account”エラーが出る場合があります。

data
PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 
'Error calling GET https://www.googleapis.com/analytics/v3/management/accounts: 
(403) User does not have any Google Analytics account.' 
in google-api-php-client\src\Google\Http\REST.php:110
 
Stack trace:
#0 google-api-php-client\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 google-api-php-client\src\Google\Task\Runner.php(174): call_user_func_array(Array, Array)
#3 google-api-php-client\src\Google\Http\REST.php(46): Google_Task_Runner->run()
#4 google-api-php-client\src\Google\Client.php(593): 
   Google_Http_REST::exec in google-api-php-client\src\Google\Http\REST.php on line 110



これは、APIのサービスアカウントを作成した際に、用意したユーザアカウントがGoogle Analyticsに登録されてないのが原因です。


GoogleAnalytics APIを利用したプログラムを作る際、最初に下記のような形で認証を行うはずです。
まずは、ここで使用するservice_account_emailを覚えておきます。

$service_account_email = 'user@projectName.iam.gserviceaccount.com';
$key_file_location = 'xxx.p12';
 
// 生成されたclient_secrets.p12 キーファイルをロードする
$key = file_get_contents($key_file_location);
 
// GooleAPIの認証を行う
$scopes = array(Google_Service_Analytics::ANALYTICS_READONLY);
$cred = new Google_Auth_AssertionCredentials( $service_account_email, $scopes, $key );




Google Analyticsの管理画面より、ユーザ管理をクリックします。



先ほどプログラムで指定していたユーザのを追加します。


ユーザ一覧に追加されていればOKです。


この状態で、再度プログラムを実行すれば403の認証エラーは解消されます。

もし、この状態でも認証エラーが出る場合は、下記の点を確認してみてください。
・Google Developer ConsoleでGoogleAnalytics APIを有効にしているか
・Google Developer Console認証情報(アカウント)を作っているか
・認証情報をOAuth APIにしているか
・認証のしかたをp12形式にしているか


関連記事

コメントを残す

メールアドレスが公開されることはありません。