Question

I am working on solution that will stream rows into BigQuery at a rate of approx 3K-5K a second via a Apache/PHP server. Authentication will need to use PKS#12 auth ("Server to server authorization")

I can't seem to find any examples of streaming and using PKS#12 auth via the PHP client lib (examples are either in Java or Python). Does anybody have some recent/up-to-date sample code I could use to get me started?

Was it helpful?

Solution

We are using: https://github.com/google/google-api-php-client and it has the key based auth.

We are requesting a token, and that token has an expiration interval, so we know when we need to request a new one. It's valid for 1 hour.

A sample:

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");

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);

You need to provide your own inputs for $service_account_name, $key_file_location, $service_token_file_location

OTHER TIPS

You have to create a 'service account' through google console and download the private key file (.p12) on your server. And use following code

  /**
   * setup service client using key file
   * @param String $keyFileLoc location of .p12 file provided in google console
   * @param String $clientEmail client location provided in google console
   * @return Google_Service_Bigquery
   */
   function setupServiceClient($keyFileLoc,$clientEmail){
       $client = new Google_Client();
       $service = new Google_Service_Bigquery($client);
       $client->setApplicationName("My Application Name");
       $key = file_get_contents($keyFileLoc);
       $cred = new Google_Auth_AssertionCredentials(
           $clientEmail,
           array('https://www.googleapis.com/auth/bigquery'),
           $key
       ); 
       $this->client->setAssertionCredentials($cred);
       return $service;
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top