xharmas
xharmas

Reputation: 43

BigQuery PHP Call to undefined method Google_Client::setAssertionCredentials()

So basically I tried this from the second answer, but it comes with an error

Fatal error: Call to undefined method Google_Client::setAssertionCredentials() in

Here's my code

require_once 'xxx/vendor/autoload.php';
public function createGClient() {
    define("CLIENT_ID", "xxx.apps.googleusercontent.com");
    define("SERVICE_ACCOUNT_NAME","[email protected]");
    define("KEY_FILE",'xxx');

    define("PROJECT_ID","xxx");
    define("DATASET_ID","xxx");
    define("TABLE_ID","xxx");

    $this->client = new Google_Client();
    $this->client->setApplicationName("Test");  

    $key = file_get_contents(KEY_FILE);
    $this->client->setAssertionCredentials(
    Google_Auth_AssertionCredentials(SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/bigquery'), $key, "notasecret"));
    $this->client->setClientId(CLIENT_ID);
    $this->service = new Google_Service_Bigquery($this->client);
}

public function runQuery() {
    // To see the a list of tables  
    print_r($this->service->tables->listTables(PROJECT_ID, DATASET_ID));

    // To see details of a table
    print_r($this->service->tables->get(PROJECT_ID, DATASET_ID, TABLE_ID));

    // To query a table
    $jobs = $this->service->jobs;
    $query = new Google_Service_Bigquery_QueryRequest();
    $query->setQuery("SELECT * FROM wherever;");
    $response = $jobs->query(PROJECT_ID, $query);
    print_r($response);
}

I already install everything from the guide/doc. Can someone help me because I tried everything and it doesnt work, thanks a lot.

Upvotes: 3

Views: 4774

Answers (1)

Pentium10
Pentium10

Reputation: 207830

No longer use that version of the PHP library as it's outdated and lacks documentation.

There is a newer one linked below it works with the need to setup the service account default credentials see lines with putenv and useApplicationDefaultCredentials(). This is a working code I have using the library https://github.com/google/google-api-php-client You need to obtain your service account key file from the console: https://console.cloud.google.com/iam-admin/serviceaccounts/

composer.json

{
    "require": {
        "google/cloud": "^0.13.0",
        "google/apiclient": "^2.0"
    }
}

php file

# Imports the Google Cloud client library
use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\ServiceBuilder;

$query="SELECT repository_url, 
       repository_has_downloads 
FROM   [publicdata:samples.github_timeline]
LIMIT  10";
$client = new Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS='.dirname(__FILE__) . '/.ssh/dummyname-7f0004z148e1.json');//this can be created with other ENV mode server side
$client->useApplicationDefaultCredentials();

$builder = new ServiceBuilder([
                'projectId' => 'edited',
        ]);

        $bigQuery = $builder->bigQuery();

        $job = $bigQuery->runQueryAsJob($query);
        $info=$job->info();
//      print_r($info);
//      exit;
        $queryResults = $job->queryResults();

        /*$queryResults = $bigQuery->runQuery(
            $query,
            ['useLegacySql' => true]);*/

        if ($queryResults->isComplete()) 
        {
            $i = 0;
            $rows = $queryResults->rows();

            foreach ($rows as $row) 
            {
                $i++;

                $result[$i] = $row;
            }
        } 
        else 
        {
            throw new Exception('The query failed to complete');
        }

        print_r($result);

Upvotes: 1

Related Questions