Я использую Google Analytics Managment API Ссылки пользователя учетной записи: список
Я пытаюсь получить список пользователей учетной записи ...
try {
$accountUserlinks = $analytics->management_accountUserLinks->listManagementAccountUserLinks('123456');
}
catch (apiServiceException $e) {
print 'There was an Analytics API service error ' . $e->getCode() . ':' . $e->getMessage();
} catch (apiException $e) {
print 'There was a general API error ' . $e->getCode() . ':' . $e->getMessage();
}
Я получаюследующая ошибка
{"error": {"errors": [{"domain": "global", "reason": "достаточное количество разрешений", "сообщение": "недостаточное разрешение"}], "code": 403, "message": "Недостаточно прав доступа"}}
В Google Analytics я установил все разрешения
Изменить совместную чтение и анализ Управление пользователями
Например:
$analytics->management_goals ->listManagementGoals - work
$analytics->management_accountUserLinks>listManagementAccountUserLinks - get 403 insufficientPermissions error
Как это исправить?
AnalyticsServiceProvider
class AnalyticsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application events.
*/
public function boot()
{
$this->publishes([
__DIR__.'/../config/analytics.php' =>
config_path('analytics.php'),
]);
}
/**
* Register the service provider.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/analytics.php',
'analytics');
$this->app->bind(AnalyticsClient::class, function () {
$analyticsConfig = config('analytics');
return
AnalyticsClientFactory::createForConfig($analyticsConfig);
});
$this->app->bind(Analytics::class, function () {
$analyticsConfig = config('analytics');
$this->guardAgainstInvalidConfiguration($analyticsConfig);
$client = app(AnalyticsClient::class);
return new Analytics($client, $analyticsConfig['view_id']);
});
$this->app->alias(Analytics::class, 'laravel-analytics');
}
protected function guardAgainstInvalidConfiguration(array
$analyticsConfig = null)
{
if (empty($analyticsConfig['view_id'])) {
throw InvalidConfiguration::viewIdNotSpecified();
}
if
(is_array($analyticsConfig['service_account_credentials_json'])) {
return;
}
if (!
file_exists($analyticsConfig['service_account_credentials_json']))
{
throw InvalidConfiguration::credentialsJsonDoesNotExist
($analyticsConfig['service_account_credentials_json']);
}
}
}
analytics.php
return [
/*
* The view id of which you want to display data.
*/
'view_id' => env('ANALYTICS_VIEW_ID'),
/*
* Path to the client secret json file. Take a look at the README
of this package
* to learn how to get this file. You can also pass the credentials
as an array
* instead of a file path.
*/
'service_account_credentials_json' =>
storage_path('app/analytics/service-account-credentials.json'),
/*
* The amount of minutes the Google API responses will be cached.
* If you set this to zero, the responses won't be cached at all.
*/
'cache_lifetime_in_minutes' => 60 * 24,
/*
* Here you may configure the "store" that the underlying
Google_Client will
* use to store it's data. You may also add extra parameters that
will
* be passed on setCacheConfig (see docs for google-api-php-
client).
*
* Optional parameters: "lifetime", "prefix"
*/
'cache' => [
'store' => 'file',
],
];
учетные данные службы-аккаунта
{
"type": "service_account",
"project_id": "buyers-analytic",
"private_key_id": "*****",
"private_key": "-----BEGIN PRIVATE KEY-----\*******",
"client_email": "buyeranalytic@buyers-
analytic.iam.gserviceaccount.com",
"client_id": "***********",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url":
"https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url":
"https://www.googleapis.com/robot/v1/metadata/x509/***.iam.gservi
ceaccount.com"
}