【问题标题】:Google BigQuery API PHP CredentialGoogle BigQuery API PHP 凭据
【发布时间】:2016-11-03 08:19:21
【问题描述】:

我想实现 Google BigQuery API,以便可以在 BigQuery 中从我的 PHP 代码执行查询。

首先我通过以下命令安装了客户端库:

composer require google/cloud

其次,我通过以下命令安装了 Google Cloud SDK:

curl https://sdk.cloud.google.com | bash

然后我运行这个命令来创建凭证:

gcloud beta auth application-default login

所有过程都成功,运行凭据请求后,我收到以下消息:

Credentials saved to file: 

[/home/some/my/dir/application_default_credentials.json]

These credentials will be used by any library that requests
Application Default Credentials.

然后我想在 PHP 上运行这段代码:

# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

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

# Your Google Cloud Platform project ID
$projectId = 'PROJECT ID';

# Instantiates a client
$bigquery = new BigQueryClient([
    'projectId' => $projectId
]);

# The name for the new dataset
$datasetName = 'my_new_dataset';

# Creates the new dataset
$dataset = $bigquery->createDataset($datasetName);

echo 'Dataset ' . $dataset->id() . ' created.';

但不幸的是,我收到以下消息错误:

Fatal error: Uncaught exception 'Google\Cloud\Exception\ServiceException' with message 'Could not load the default credentials

所以我的问题是:出了什么问题,我必须做什么?

谢谢

【问题讨论】:

  • 您的费率很低。对 SO 很重要,您必须使用已发布答案左侧、投票下方的勾号来标记已接受的答案。这将提高您的费率。通过访问此链接了解其工作原理:meta.stackoverflow.com/questions/5234/…

标签: php google-bigquery


【解决方案1】:

默认凭据由Google APIs Client Library for PHP 2.0.0 及更高版本提供。要使用它们,请调用 useApplicationDefaultCredentials:

$client = new Google_Client();
$client->useApplicationDefaultCredentials();

然后,使用凭据访问 API 服务,如下所示:

$client->setScopes(['https://www.googleapis.com/auth/books']);
$service = new Google_Service_Books($client);
$results = $service->volumes->listVolumes('Henry David Thoreau');

我建议查看我提供的链接有更多选项,我们建议使用service accounts

【讨论】:

  • 嗨@Pentium10 感谢您的回复,我已尝试添加 $client->useApplicationDefaultCredentials();但我得到了这个回报:致命错误:找不到类“Google_Client”?
  • 确保你运行库的正确版本composer require google/apiclient:^2.0github有很多例子
猜你喜欢
  • 2018-09-23
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
相关资源
最近更新 更多