无法使用API键从PHP脚本访问Firestore文档



我是Firebase/Firestore的新手。我在Firestore中创建了两个访问相同数据的项目。一个是在android中,另一个是php中的web应用程序。我可以在安卓应用程序中访问数据,但在PHP应用程序中我不得不在凭证页面中创建一个API密钥。我最初想试用Firestore,但无法访问这些文档。它给出一个错误

致命错误:未捕获的运行时异常:凭据获取程序未在C:\examplep\htdocs\FirestoreProject\vendor\Google\Auth\src\FetchAuthTokenCache.php中实现Google\Auth\UpdateMetadataInterface:190堆栈跟踪:#0 C:\examplep\ htdocs\FirestoreProject\vendor\Google\gax\src\CredentialsWrapper.php(197(:Google\Auth\WetchAuthTokenCache->updateMetadata(数组,'https://firesto...'(#1[内部函数]:Google\ApiCore\CredentialsWrapper->Google\ApiCore{closure}(Object(stdClass((#2 C:\examplep\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\ServerStreamingCall.php(44(:grpc\Call->startBatch(数组(#3 C:\examplep\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\BaseStub.php(364(:grpc\ServerStreamingCall->start(Object(Google\Cloud\Firestore\V1\BatchGetDocumentsRequest(,Array,Array(#4 C:\examplep\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\BaseStub.php(589(:grpc\BaseStub->Grpc{closure}('/google.firesto…',Object(google\Cloud\Firestore\V1\BatchGetDocumentsRequest(,Array,Array,Array(#5 C:\examplep\htdocs\FirestoreProject\ve in C:\examplep\tdocs\Firestore Project\vendor\google\auth\src\FetchAuthTokenCache.php on line 190

我已经安装了composer并更新了依赖项和gRPC。

我的index.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;
?>

我的Firestore.php文件读取

<?php
use GoogleCloudCoreExceptionGoogleException;
use GoogleCloudFirestoreFirestoreClient;

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

?>

我试着激活凭据页面,但他们要求信用卡计费以开始试用,但我没有。我们不能在不激活凭据的情况下测试数据库吗?测试和试用版之间的区别是什么?

请帮助

关于

Sanjish

在创建服务帐户并在命令提示符下使用set GOOGLE_APPLICATION_CREDENTIALS 指向key.json文件后,我制作了一个test.php文件

<?php
require_once 'vendor/autoload.php';

session_start();

use GoogleCloudFirestoreFirestoreClient;

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_APPLICATION_CREDENTIALS,因为这不是为用户Apache(或httpd(定义的。为了解决这个问题,我使用这个代码块修改了初始化


$db = new FirestoreClient([
'keyFilePath' => '/path/to/credentials.json',
'projectId' => 'my-project-id'
]);

使用keyFilePath(此处记录了此参数(,Firestore库将使用路径字符串而不是环境变量来搜索GCP凭据

最新更新