Php Bigquery API Resolution

I am trying to access the bigquery api using the PHP library, but I always have this error:

Fatal error: throw a "Google_Service_Exception" exception with the message "GET call error https://www.googleapis.com/bigquery/v2/projects/primeval-shadow-571/datasets/Test/tables : (401) Login Required 'in. ..

I have a web application that needs to get some data from a table in bigquery. The remote account is not mine, but it was created by some guy (and I don’t have a login and password to log in), which gave me a file .jsonthat included thoose parameters:

auth_uri, 
client_secret, 
token_uri, 
client_email, 
client_x509_cert_url, 
client_id and 
auth_provider_x509_cert_url.

Here is my PHP code:

<?php

require_once 'Google/Client.php';
require_once 'Google/Service/Bigquery.php';

$client = new Google_Client();
$client->setAuthConfigFile('google-config.json');

$service = new Google_Service_Bigquery($client);

$service->tables->listTables('primeval-shadow-571', 'Test');

But finally, I got the error above.

Can someone tell me where I am going wrong?

P.S. google api , .

.

+1
1

, :

session_start();

define('PROJECT_ID', 'edited');
define('DATASET_ID', 'edited');
define('API_KEY', 'edited');

$client_id = 'edited';
$service_account_name = 'edited';
$key_file_location = '.ssh/privatekey-bigquery.p12';
$service_token_file_location = 'bigquery_current_service_token.json';

set_include_path("google-api-php/src/" . PATH_SEPARATOR . get_include_path());
require_once 'google-api-php/src/Google/Client.php';
require_once 'google-api-php/src/Google/Service/Bigquery.php';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
//$client->setDeveloperKey(API_KEY);

if (!is_file($service_token_file_location)) {
    if (!is_writable($service_token_file_location)) {
        @chmod($service_token_file_location, 0777);
        if (!is_writable($service_token_file_location)) {
            die('Service token file is not writable: ' . $service_token_file_location);
        }
    }
    file_put_contents($service_token_file_location, '');
} else {
    if (!is_writable($service_token_file_location)) {
        @chmod($service_token_file_location, 0777);
        if (!is_writable($service_token_file_location)) {
            die('Service token file is not writable: ' . $service_token_file_location);
        }
    }
}
$service_token = @file_get_contents($service_token_file_location);
if (!empty($service_token)) {
    $client->setAccessToken($service_token);
}
if (!file_exists($key_file_location)) {
    die('Key file is missing: ' . $key_file_location);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
        $service_account_name, array(
    'https://www.googleapis.com/auth/bigquery',
        ), $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}
$service_token = $client->getAccessToken();
file_put_contents($service_token_file_location, $service_token);

// start using $client
+1

All Articles