Php Google分析API权限不足403错误

Php Google分析API权限不足403错误,php,google-api,google-analytics-api,google-api-php-client,Php,Google Api,Google Analytics Api,Google Api Php Client,我使用谷歌分析管理API 我正在尝试获取帐户用户列表 try { $accountUserlinks = $analytics->management_accountUserLinks->listManagementAccountUserLinks('123456'); } catch (apiServiceException $e) { print 'There was an Analytics API service error ' . $e->

我使用谷歌分析管理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();
}
我得到以下错误

{“错误”:{“错误”: [{“域”:“全局”,“原因”:“权限不足”,“消息”: 权限不足“}],“代码”:403,“消息”:“权限不足”}

在谷歌分析中,我设置了所有权限

编辑协作读取和分析管理用户

例如:

$analytics->management_goals ->listManagementGoals  - work

$analytics->management_accountUserLinks>listManagementAccountUserLinks -  get 403 insufficientPermissions error
如何修复它

分析服务提供商

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

服务帐户凭据

 {
"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"
}
确切地说,这意味着你没有权限去做你想做的事情

需要以下作用域

需要以下作用域

为了使用第一种方法,您需要修复身份验证并请求用户的其他作用域。一旦向请求中添加了其他作用域,您就需要再次对用户进行身份验证

例如:

function initializeAnalytics()
{
  // Creates and returns the Analytics Reporting service object.

  // Use the developers console and download your service account
  // credentials in JSON format. Place them in this directory or
  // change the key file location if necessary.
  $KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';

  // Create and configure a new client object.
  $client = new Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly', 'https://www.googleapis.com/auth/analytics.manage.users.readonly']);
  $analytics = new Google_Service_Analytics($client);

  return $analytics;
}

我不知道你从哪里得到你正在使用的代码。我建议使用。服务帐户需要在帐户级别授予访问权限。我已经添加了一个示例,演示了如何设置作用域。你只需要在你的代码中找到你设置范围的地方,我在你发布的任何东西中都看不到。我还创建了一些示例

请尝试插入分析帐户设置中的帐户ID,而不是此行代码中的“123456”:

... management_accountUserLinks->listManagementAccountUserLinks('**123456**');

此外,服务帐户需要具有帐户级别的访问权限。

我不理解这一部分:为了使用第一种方法,您需要修复身份验证并请求用户的其他作用域。一旦向请求中添加了其他作用域,您就需要再次对用户进行身份验证。如何修复身份验证、添加其他作用域并再次对用户进行身份验证?请编辑您的问题并包含您用于验证应用程序的代码。你应该有一些关于范围的东西。您只需添加另一个,然后再次运行。欢迎使用stack,我可以看到这是您第一次在这里。你可能想读书。我们不是一个论坛,您提供给我们的所有信息都必须添加到您自己的问题中,您不应该将其作为答案发布,除非它是您问题的实际答案。您应该编辑您的问题并添加其他信息。
function initializeAnalytics()
{
  // Creates and returns the Analytics Reporting service object.

  // Use the developers console and download your service account
  // credentials in JSON format. Place them in this directory or
  // change the key file location if necessary.
  $KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';

  // Create and configure a new client object.
  $client = new Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly', 'https://www.googleapis.com/auth/analytics.manage.users.readonly']);
  $analytics = new Google_Service_Analytics($client);

  return $analytics;
}
... management_accountUserLinks->listManagementAccountUserLinks('**123456**');