I am new to GCP. I have created a Bigquery Dataset following this guide.
Now, I am trying to fetch those data with PHP. I have followed this other guide for that.
Here is my code:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Google\Cloud\BigQuery\BigQueryClient;
use venor\google\cloud\Core\src\ExponentialBackoff;
$projectId = 'abcd';
$query = 'SELECT * FROM `abcd.TestData.BigD2019` LIMIT 1000';
$bigQuery = new BigQueryClient([
'projectId' => $projectId,
]);
$jobConfig = $bigQuery->query($query);
$job = $bigQuery->startQuery($jobConfig);
$backoff = new ExponentialBackoff(10);
$backoff->execute(function () use ($job) {
print('Waiting for job to complete' . PHP_EOL);
$job->reload();
if (!$job->isComplete()) {
throw new Exception('Job has not yet completed', 500);
}
});
$queryResults = $job->queryResults();
$i = 0;
foreach ($queryResults as $row) {
printf('--- Row %s ---' . PHP_EOL, ++$i);
foreach ($row as $column => $value) {
printf('%s: %s' . PHP_EOL, $column, json_encode($value));
}
}
printf('Found %s row(s)' . PHP_EOL, $i);
?>
While Trying to run this, I am getting below error -
Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' with message '{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } } ' in C:\xampp\htdocs\vendor\google\cloud\Core\src\RequestWrapper.php:336 Stack trace: #0 C:\xampp\htdocs\vendor\google\cloud\Core\src\RequestWrapper.php(189): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException)) #1 C:\xampp\htdocs\vendor\google\cloud\Core\src\RestTrait.php(96): Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array) #2 C:\xampp\htdocs\vendor\google\cloud\BigQuery\src\Connection\Rest.php(218): Google\Cloud\BigQuery\Connection\Rest->send('jobs', 'insert', Array) #3 C:\xampp\htdocs\vendor\google\cloud\BigQuery\src\BigQueryClient.php(370): Google\Cloud\BigQuery\Connection\Rest->insertJob(Array) #4 C: in C:\xampp\htdocs\vendor\google\cloud\Core\src\RequestWrapper.php on line 336
I am not able to figure out the exact problem. I am guessing this is possibly due to Authentication.
Identity and API access
. – John Hanley