[英]How to use streaming with BigQuery from PHP using PKS#12 auth
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. 我正在研究将通过Apache / PHP服务器以约3K-5K每秒的速度将行流式传输到BigQuery的解决方案。 Authentication will need to use PKS#12 auth (" Server to server authorization ")
身份验证将需要使用PKS#12 auth(“ 服务器到服务器授权 ”)
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). 我似乎找不到任何通过PHP客户端库通过流和使用PKS#12身份验证的示例(示例是Java或Python)。 Does anybody have some recent/up-to-date sample code I could use to get me started?
有人有一些我可以用来入门的最新/最新示例代码吗?
We are using: https://github.com/google/google-api-php-client and it has the key based auth. 我们正在使用: https : //github.com/google/google-api-php-client ,它具有基于密钥的身份验证。
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.
有效期为1小时。
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
您需要为
$service_account_name
, $key_file_location
, $service_token_file_location
提供自己的输入
You have to create a 'service account' through google console and download the private key file (.p12) on your server. 您必须通过Google控制台创建一个“服务帐户”,然后在您的服务器上下载私钥文件(.p12)。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.