无法使用API密钥从PHP脚本访问Firestore文档
我是Firebase/Firestore的新手。我在Firestore中创建了两个访问相同数据的项目。 一个是android,另一个是php的web应用。我可以在android应用程序中访问数据,但可以在PHP应用程序中访问 我必须在凭证页面中创建一个API密钥。我最初想试用Firestore,但无法访问文档。它给出了一个错误 致命错误:未捕获运行时异常:凭据获取程序未在C:\xampp\htdocs\FirestoreProject\vendor\Google\Auth\src\FetchAuthTokenCache.php:190堆栈跟踪:#0 C:\xampp\htdocs\FirestoreProject\vendor\Google\gax\src\credentialswapper.php(197)中实现Google\Auth\updateMetadata接口:Google\Auth\FetchAuthTokenCache->updateMetadata(数组,'https://firesto...“)#1[内部函数]:Google\ApiCore\credentialswapper->Google\ApiCore{closure}(对象(stdClass))#2 C:\xampp\htdocs\FirestoreProject\vendor\grpc\grc\lib\ServerStreamingCall.php(44):grpc\Call->->startBatch(数组)#3 C:\xampp\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\BaseStub.php(364):grpc\ServerStreamingCall->start(Object(Object(Google\Cloud\Firestore\V1\BatchGetDocumentsRequest),Array,Array)#4 C:\xampp\htdocs\FirestoreProject\vendor\grpc\grpc\grpc\grpc\src\lib\BaseStub.php(589):grpc\BaseStub->->\BaseStub->{closure}('/Google.firesto…),Object(Google\Cloud\Firestore\V1\BatchGetDocumentsRequest),数组,数组,数组)#5 C:\xampp\htdocs\FirestoreProject\ve中的C:\xampp\htdocs\FirestoreProject\vendor\Google\auth\src\FetchAuthTokenCache.php,第190行 我已经安装了composer并更新了依赖项和gRPC 我的index.php文件读取无法使用API密钥从PHP脚本访问Firestore文档,php,google-cloud-firestore,Php,Google Cloud Firestore,我是Firebase/Firestore的新手。我在Firestore中创建了两个访问相同数据的项目。 一个是android,另一个是php的web应用。我可以在android应用程序中访问数据,但可以在PHP应用程序中访问 我必须在凭证页面中创建一个API密钥。我最初想试用Firestore,但无法访问文档。它给出了一个错误 致命错误:未捕获运行时异常:凭据获取程序未在C:\xampp\htdocs\FirestoreProject\vendor\Google\Auth\src\FetchAu
<?php
session_start();
require_once 'vendor/autoload.php';
require_once "Firestore.php";
$apiKey = 'WWWWWWWWWWWWWWWWWWWWWW';
$url ='https://console.firebase.google.com/project/prject-ID/firestore?
key=Aasdads';
$fs = new Firestore('Users');
print_r($fs->getDocument('9427349073'));
return;
?>
<?php
use Google\Cloud\Core\Exception\GoogleException;
use Google\Cloud\Firestore\FirestoreClient;
class Firestore
{
protected $db;
protected $name; // $name is name of collection
public function __construct(string $collection) // $collection =
// 'Users'
{
if (isset($this)) {
try {
$this->db = new FirestoreClient([
'projectId' => 'XXXXXXXXXXXXXXXXXXX'
]);
$this->name = $collection;
} catch (GoogleException $e) {
print_r($e->getMessage());
}
}else{
}
}
public function getDocument(string $name) // $name is name of document
{
return $this->db->collection($this->name)->document($name)-
>snapshot()->data();
}
}
?>
我的Firestore.php文件读取
<?php
session_start();
require_once 'vendor/autoload.php';
require_once "Firestore.php";
$apiKey = 'WWWWWWWWWWWWWWWWWWWWWW';
$url ='https://console.firebase.google.com/project/prject-ID/firestore?
key=Aasdads';
$fs = new Firestore('Users');
print_r($fs->getDocument('9427349073'));
return;
?>
<?php
use Google\Cloud\Core\Exception\GoogleException;
use Google\Cloud\Firestore\FirestoreClient;
class Firestore
{
protected $db;
protected $name; // $name is name of collection
public function __construct(string $collection) // $collection =
// 'Users'
{
if (isset($this)) {
try {
$this->db = new FirestoreClient([
'projectId' => 'XXXXXXXXXXXXXXXXXXX'
]);
$this->name = $collection;
} catch (GoogleException $e) {
print_r($e->getMessage());
}
}else{
}
}
public function getDocument(string $name) // $name is name of document
{
return $this->db->collection($this->name)->document($name)-
>snapshot()->data();
}
}
?>
我尝试激活凭据页面,但他们要求使用信用卡进行计费以开始试用,而我没有。我们不能在不激活凭据的情况下测试数据库吗?测试和试用使用之间有什么区别
请帮忙
问候
桑吉什
在创建了一个服务帐户并使用set GOOGLE_APPLICATION_凭据指向命令提示符上的key.json文件后,我创建了一个test.php文件
<?php
require_once 'vendor/autoload.php';
session_start();
use Google\Cloud\Firestore\FirestoreClient;
initialize();
function initialize()
{
// Create the Cloud Firestore client
$db = new FirestoreClient([
'projectId' => 'Valbhai-xxx'
]);
$usersRef = $db->collection('Users'); // This works
$snapshot = $usersRef->documents(); // This does not work , gives
// the same error
foreach ($snapshot as $user) {
printf('User: %s' . PHP_EOL, $user->client_name());
}
}
?>
我在我的计算机上重现了您的问题,我注意到这是因为PHP忽略了环境变量
GOOGLE\u APPLICATION\u CREDENTIALS
,因为这不是为用户Apache(或httpd)定义的。为了解决此问题,我使用此代码块修改了初始化
$db = new FirestoreClient([
'keyFilePath' => '/path/to/credentials.json',
'projectId' => 'my-project-id'
]);
使用
keyFilePath
(记录此参数),Firestore library将使用路径字符串搜索GCP凭据,而不是使用环境变量我在我的计算机上重现了您的问题,我注意到这是因为PHP忽略了环境变量GOOGLE\u APPLICATION\u凭据,因为这不是为用户Apache(或httpd)定义的为了解决这个问题,我使用这个代码块修改了初始化
$db = new FirestoreClient([
'keyFilePath' => '/path/to/credentials.json',
'projectId' => 'my-project-id'
]);
使用
keyFilePath
(记录此参数),Firestore library将使用路径字符串搜索GCP凭据,而不是使用环境变量您好,您是否按照PHP中所述定义了凭据,PHP是后端软件,必须使用服务帐户凭据才能使用FirestoreI我遵照您的建议,创建了服务帐户。It奏效了。谢谢你,它部分奏效了。$usersRef=$db->collection('Users');->这很有效,但是$doc=$usersRef->documents();给出相同的错误您可以更新问题中的代码以了解哪些有效,哪些无效吗?您好,您已经按照PHP中提到的定义了您的凭据,PHP是一个后端软件,必须使用服务帐户凭据才能使用FireStore我按照您的建议Jan创建了一个服务帐户。成功了。谢谢lotIt部分起作用。$usersRef=$db->collection('Users');->这是有效的,但是$doc=$usersRef->documents();给出了相同的错误您可以更新问题中的代码以了解哪些有效,哪些无效吗?FirestoreClient()只接受1个参数,不接受2@SANJISHKUMAR你确定吗?你有机会测试它吗?keyFilePath
记录在FirestoreClient()上,只接受1个参数,不接受2@SANJISHKUMAR你确定吗?你有机会测试它吗?keyFilePath
记录在