Google BigQuery API PHP凭证

编程入门 行业动态 更新时间:2024-10-05 15:33:30
本文介绍了Google BigQuery API PHP凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

作曲家需要谷歌/云

第二,我已经通过以下命令安装了Google Cloud SDK:

curl sdk.cloud .google | bash

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

gcloud beta auth application-default login

所有的过程都是成功的,并且在运行证书请求之后,我得到以下消息:

保存到文件的凭证: [/home/some/my/dir/application_default_credentials.json] 这些凭据将被任何请求应用默认凭证的库使用。

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

#包含使用composer require__DIR__安装的库的自动加载器。 /vendor/autoload.php; #导入Google Cloud客户端库使用Google\Cloud\BigQuery\BigQueryClient; #您的Google Cloud Platform项目ID $ projectId ='PROJECT ID'; #实例化一个客户端 $ bigquery = new BigQueryClient(['projectId'=> $ projectId ]); #新数据集的名称 $ datasetName ='my_new_dataset'; #创建新的数据集 $ dataset = $ bigquery-> createDataset($ datasetName); echo'数据集'。 $ dataset-> id()。 '创造'。

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

致命错误:Uncaught异常'Google\Cloud\Exception\ServiceException'带消息'无法加载默认凭证

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

谢谢

解决方案

默认凭据由适用于PHP的Google API客户端库,2.0.0及更新版本。要使用它们,请调用useApplicationDefaultCredentials:

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

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

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

我建议查看我给它的链接有更多选项,我们推荐使用服务帐户。

I want to implement Google BigQuery API so I can execute query from my PHP code in BigQuery.

First I have installed the client library by following command:

composer require google/cloud

Second I have installed the Google Cloud SDK by following command:

curl sdk.cloud.google | bash

Then I run this command to create the credential:

gcloud beta auth application-default login

All the process is success and after run credential request I get the following message:

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.

Then I want to run this code on 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.';

But unfortunately I got following message error:

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

So my question is: whats wrong and what must I do?

Thank you

解决方案

Default credentials are provided by the Google APIs Client Library for PHP, versions 2.0.0 and newer. To use them, call useApplicationDefaultCredentials:

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

Then, use the credentials to access an API service as follows:

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

I am suggesting checking out the link I gave it has much more options and we recommend using service accounts.

更多推荐

Google BigQuery API PHP凭证

本文发布于:2023-11-28 04:26:10,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:凭证   BigQuery   Google   PHP   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!