[GoogleAnalytics API]Google_Service_Exception(Insufficient Permission)例外が出るときの対応

GoogleAnalyticsでは、APIを使用することでデータのバッチ登録が可能です。

データを検索(取得)は可能だだが、データファイルのインポートするためにREST APIをコールしたとき、Insufficient Permissionエラーが出る場合があります。

Fatal error: Uncaught exception 'Google_Service_Exception' with message 
'Error calling POST https://www.googleapis.com/upload/analytics/v3/management/accounts/...: 
(403) Insufficient Permission' in .../google-api-php-client-1-master/src/Google/Http/REST.php:110



この場合、Google_Auth_AssertionCredentials()へ渡すパラメータが不正な場合があります。
以下のように、ANALYTICS_EDIT権限を付与するとエラーが解消されるかもしれません。

$scopes = array(Google_Service_Analytics::ANALYTICS_READONLY);
$cred = new Google_Auth_AssertionCredentials( $email, $scopes, $key );




$scopes = array(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Analytics::ANALYTICS_EDIT);
$cred = new Google_Auth_AssertionCredentials( $email, $scopes, $key );



プログラム中でconst値を使っていない場合は、以下のような変更となります。

$scopes = array('https://www.googleapis.com/auth/analytics.readonly');
$cred = new Google_Auth_AssertionCredentials( $email, $scopes, $key );




$scopes = array('https://www.googleapis.com/auth/analytics.readonly', 
                'https://www.googleapis.com/auth/analytics.edit' );
$cred = new Google_Auth_AssertionCredentials( $email, $scopes, $key );

関連記事

コメントを残す

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