我正在为4管理API使用PHP库。但我无法拿到账户清单。然而,我认为这是因为我没有通过auth access_token。但我不知道该怎么做。
require 'vendor/autoload.php';
use Google\Analytics\Admin\V1alpha\AnalyticsAdminServiceClient;
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
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": "*******************"
}发布于 2022-07-05 07:44:46
您不应该应用访问令牌,应该让客户端库获取它自己的访问令牌。
require 'vendor/autoload.php';
use Google\Analytics\Admin\V1alpha\AnalyticsAdminServiceClient;
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;
}https://stackoverflow.com/questions/72864508
复制相似问题