如何通过访问令牌谷歌分析4管理api



我使用PHP库的谷歌分析4管理API。但是我拿不到账户清单。然而,我认为这是因为我没有传递认证access_token。但我不确定如何才能做到。

require 'vendor/autoload.php';
use GoogleAnalyticsAdminV1alphaAnalyticsAdminServiceClient;
use GoogleAnalyticsDataV1betaBetaAnalyticsDataClient;
use GoogleAnalyticsDataV1betaDateRange;
use GoogleAnalyticsDataV1betaDimension;
use GoogleAnalyticsDataV1betaMetric;
putenv('GOOGLE_APPLICATION_CREDENTIALS=config.json');
$access_token = "**********";
$client = new AnalyticsAdminServiceClient(['access_token' => $access_token]); // is this the correct way to pass the token?
$accounts = $client->listAccountSummaries();

使用访问令牌访问此API的正确方法是什么?

我的json文件有以下凭证,

{
"type": "service_account",
"project_id": "**********************",
"private_key_id": "**********************",
"private_key": "**********************",
"client_email": "**********************.iam.gserviceaccount.com",
"client_id": "**********************",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "*******************"
}

您不应该应用访问令牌,您应该让客户端库自己获取。

require 'vendor/autoload.php';
use GoogleAnalyticsAdminV1alphaAnalyticsAdminServiceClient;
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client = new AnalyticsAdminServiceClient();
$accounts = $client->listAccounts();
foreach ($accounts as $account) {
print 'Found account: ' . $account->getName() . PHP_EOL;
}

php-analytics-admin #样品

最新更新