如何使用 google-api-php-client 查询 Google Analytics API
How to make query to Google Analytics API using google-api-php-client
我尝试将 Google 分析与 google-api-php-client
一起使用
我使用了版本 1(较旧,但在 Analytics Core Reporting API and API Client Library for PHP 示例中)和版本 2.
"google/apiclient": "1.0.*@beta":
代码:
$service_account_email = 'pd*******@crucial-subset-******.iam.gserviceaccount.com';
$key_file_location = \Yii::getAlias('@common/data/google-analitics/DEMO PROJECT-*******.p12');
// Create and configure a new client object.
$client = new \Google_Client();
$client->setApplicationName("HelloAnalytics");
$analytics = new \Google_Service_Analytics($client);
// Read the generated client_secrets.p12 key.
$key = file_get_contents($key_file_location);
$cred = new \Google_Auth_AssertionCredentials(
$service_account_email,
array(\Google_Service_Analytics::ANALYTICS_READONLY),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$results = $analytics->data_ga->get(
'ga:10****',
'7daysAgo',
'today',
'ga:sessions');
回复:
Exception 'Google_Service_Exception' with message
'Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A10****&start-date=7daysAgo
&end-date=today&metrics=ga%3Asessions:
(403) User does not have any Google Analytics account.'
google/apiclient:^2.0.0@RC:
代码:
$client = new \Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . \Yii::getAlias('@common/data/google- analitics/DEMO PROJECT-************.json'));
$client->useApplicationDefaultCredentials();
$user_to_impersonate = 'denis********@******.com';
$client->setSubject($user_to_impersonate);
$scopes = [\Google_Service_Analytics::ANALYTICS_READONLY];
$client->setScopes($scopes);
if( $client->isAccessTokenExpired();) {
$client->refreshTokenWithAssertion();
}
$service = new \Google_Service_Analytics($client);
$results = $service->data_ga->get(
'ga:******',
'7daysAgo',
'today',
'ga:sessions');
回复:
Exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `POST https://www.googleapis.com/oauth2/v4/token` resulted in a `401 Unauthorized` response:
{
"error": "unauthorized_client",
"error_description": "Unauthorized client or scope in request."
}
我尝试将 Google 分析与 google-api-php-client
一起使用
我使用了版本 1(较旧,但在 Analytics Core Reporting API and API Client Library for PHP 示例中)和版本 2.
"google/apiclient": "1.0.*@beta": 代码:
$service_account_email = 'pd*******@crucial-subset-******.iam.gserviceaccount.com';
$key_file_location = \Yii::getAlias('@common/data/google-analitics/DEMO PROJECT-*******.p12');
// Create and configure a new client object.
$client = new \Google_Client();
$client->setApplicationName("HelloAnalytics");
$analytics = new \Google_Service_Analytics($client);
// Read the generated client_secrets.p12 key.
$key = file_get_contents($key_file_location);
$cred = new \Google_Auth_AssertionCredentials(
$service_account_email,
array(\Google_Service_Analytics::ANALYTICS_READONLY),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$results = $analytics->data_ga->get(
'ga:10****',
'7daysAgo',
'today',
'ga:sessions');
回复:
Exception 'Google_Service_Exception' with message
'Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A10****&start-date=7daysAgo
&end-date=today&metrics=ga%3Asessions:
(403) User does not have any Google Analytics account.'
google/apiclient:^2.0.0@RC: 代码:
$client = new \Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . \Yii::getAlias('@common/data/google- analitics/DEMO PROJECT-************.json'));
$client->useApplicationDefaultCredentials();
$user_to_impersonate = 'denis********@******.com';
$client->setSubject($user_to_impersonate);
$scopes = [\Google_Service_Analytics::ANALYTICS_READONLY];
$client->setScopes($scopes);
if( $client->isAccessTokenExpired();) {
$client->refreshTokenWithAssertion();
}
$service = new \Google_Service_Analytics($client);
$results = $service->data_ga->get(
'ga:******',
'7daysAgo',
'today',
'ga:sessions');
回复:
Exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `POST https://www.googleapis.com/oauth2/v4/token` resulted in a `401 Unauthorized` response:
{
"error": "unauthorized_client",
"error_description": "Unauthorized client or scope in request."
}