1

我正在研究通过 Apache/PHP 服务器以每秒大约 3K-5K 的速率将行流式传输到 BigQuery 的解决方案。身份验证将需要使用 PKS#12 auth(“服务器到服务器授权”)

我似乎无法通过 PHP 客户端库找到任何流式传输和使用 PKS#12 身份验证的示例(示例使用 Java 或 Python)。有没有人有一些我可以用来帮助我开始的最近/最新的示例代码?

4

2 回答 2

0

我们正在使用:https ://github.com/google/google-api-php-client ,它具有基于密钥的身份验证。

我们正在请求一个令牌,并且该令牌有一个过期间隔,因此我们知道何时需要请求一个新令牌。有效期为 1 小时。

一个样品:

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

$service_account_name您需要为, $key_file_location,提供自己的输入$service_token_file_location

于 2014-05-15T16:42:46.453 回答
0

您必须通过 google 控制台创建一个“服务帐户”并在您的服务器上下载私钥文件 (.p12)。并使用以下代码

  /**
   * 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;
  }
于 2014-07-22T07:43:52.940 回答