Authentication with Google Analytics API request PHP
I have followed this turotial to integrate Google Analytics Data API (GA4) in my application.
This is the sample code the authentication tutorial provides: (contains 3 ways of authenticating)
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
// Authenticating with keyfile data.
$storage = new StorageClient([
'keyFile' => json_decode(file_get_contents('/path/to/keyfile.json'), true)
]);
// Authenticating with a keyfile path.
$storage = new StorageClient([
'keyFilePath' => '/path/to/keyfile.json'
]);
// Providing the Google Cloud project ID.
$storage = new StorageClient([
'projectId' => 'myProject'
]);
Then there is this sample code for making request to the API:
require 'vendor/autoload.php';
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
/**
* TODO(developer): Replace this variable with your Google Analytics 4
* property ID before running the sample.
*/
$property_id = 'YOUR-GA4-PROPERTY-ID';
// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();
// Make an API call.
$response = $client->runReport([
'property' => 'properties/' . $property_id,
'dateRanges' => [
new DateRange([
'start_date' => '2020-03-31',
'end_date' => 'today',
]),
],
'dimensions' => [new Dimension(
[
'name' => 'city',
]
),
],
'metrics' => [new Metric(
[
'name' => 'activeUsers',
]
)
]
]);
// Print results of an API call.
print 'Report result: ' . PHP_EOL;
foreach ($response->getRows() as $row) {
print $row->getDimensionValues()[0]->getValue()
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
}
I am using CodeIgniter 4 and this is my code:
public function __construct(ConnectionInterface &$db = null, ValidationInterface $validation = null) {
parent::__construct($db, $validation);
$config = [
'projectId' => 'XXX',
'keyFilePath' => 'path',
];
new StorageClient($config);
}
public function apiRequest() {
$property_id = 'xxx';
// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();
// Make an API call.
$response = $client->runReport([
'property' => 'properties/' . $property_id,
'dateRanges' => [
new DateRange([
'start_date' => '2020-03-31',
'end_date' => 'today',
]),
],
'dimensions' => [new Dimension(
[
'name' => 'city',
]
),
],
'metrics' => [new Metric(
[
'name' => 'activeUsers',
]
)
]
]);
// Print results of an API call.
print 'Report result: ' . PHP_EOL;
foreach ($response->getRows() as $row) {
print $row->getDimensionValues()[0]->getValue()
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
}
}
This is the error I am getting:
CRITICAL - 2021-08-25 13:46:20 --> Could not construct ApplicationDefaultCredentials
#0 ...\vendor\google\gax\src\CredentialsWrapper.php(132): Google\ApiCore\CredentialsWrapper::buildApplicationDefaultCredentials(NULL, Object(Google\Auth\HttpHandler\Guzzle7HttpHandler), NULL, NULL, NULL, Array)
#1 ...\vendor\google\gax\src\GapicClientTrait.php(426): Google\ApiCore\CredentialsWrapper::build(Array)
#2 ...\vendor\google\gax\src\GapicClientTrait.php(403): Google\Analytics\Data\V1beta\Gapic\BetaAnalyticsDataGapicClient->createCredentialsWrapper(NULL, Array)
#3 ...\vendor\google\analytics-data\src\V1beta\Gapic\BetaAnalyticsDataGapicClient.php(290): Google\Analytics\Data\V1beta\Gapic\BetaAnalyticsDataGapicClient->setClientOptions(Array)
#4 ...\app\Models\AnalyticsModel.php(30): Google\Analytics\Data\V1beta\Gapic\BetaAnalyticsDataGapicClient->__construct()
#5 ...\app\Controllers\Admin.php(40): App\Models\AnalyticsModel->apiRequest()
#6 ...\vendor\codeigniter4\framework\system\CodeIgniter.php(928): App\Controllers\Admin->dashboard()
#7 ...\vendor\codeigniter4\framework\system\CodeIgniter.php(436): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Admin))
#8 ...\vendor\codeigniter4\framework\system\CodeIgniter.php(336): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#9 ...\public\index.php(37): CodeIgniter\CodeIgniter->run()
#10 ...\vendor\codeigniter4\framework\system\Commands\Server\rewrite.php(45): require_once('C:\\Users\\JolanG...')
What am I doing wrong?
from Recent Questions - Stack Overflow https://ift.tt/3sN4PzB
https://ift.tt/eA8V8J
Comments
Post a Comment