【问题标题】:Cant access Firestore documents from PHP scripts with API key无法使用 API 密钥从 PHP 脚本访问 Firestore 文档
【发布时间】:2021-03-19 23:15:04
【问题描述】:

我是 Firebase/Firestore 的新手。我创建了 2 个访问 Firestore 中相同数据的项目。 一个是在 android 中,另一个是在 php 中的网络应用程序。我能够在 android 应用程序中访问数据,但在 PHP 应用程序中 我必须在凭证页面中创建一个 API 密钥。我最初想试用 Firestore,但无法访问文档。它给出了一个错误

致命错误:未捕获的 RuntimeException:凭据提取器未在 C:\xampp\htdocs\FirestoreProject\vendor\google\auth\src\FetchAuthTokenCache.php:190 堆栈跟踪:#0 C:\ 中实现 Google\Auth\UpdateMetadataInterface xampp\htdocs\FirestoreProject\vendor\google\gax\src\CredentialsWrapper.php(197): Google\Auth\FetchAuthTokenCache->updateMetadata(Array, 'https://firesto...') #1 [内部函数]: Google\ApiCore\CredentialsWrapper->Google\ApiCore{closure}(Object(stdClass)) #2 C:\xampp\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\ServerStreamingCall.php(44): Grpc\Call ->startBatch(Array) #3 C:\xampp\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\BaseStub.php(364): Grpc\ServerStreamingCall->start(Object(Google\Cloud\Firestore\V1 \BatchGetDocumentsRequest), Array, Array) #4 C:\xampp\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\BaseStub.php(589): Grpc\BaseStub->Grpc{closure}('/google. firesto...',对象(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 文件读取

<?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 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_CREDENTIALS 在命令提示符上指向 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 firestore quickstart 中所述定义了您的凭据,PHP 是一个后端软件,使用 Firestore 需要使用服务帐户凭据
  • 我听从了 Jan 的建议,创建了一个服务帐户。它成功了。非常感谢
  • 它部分工作。 $usersRef = $db->collection('Users'); -> 这可行,但 $doc = $usersRef->documents();给出同样的错误
  • 您能否更新问题中的代码以了解哪些有效,哪些无效?

标签: php google-cloud-firestore


【解决方案1】:

我在我的计算机上重现了您的问题,我注意到这是因为 PHP 忽略了环境变量 GOOGLE_APPLICATION_CREDENTIALS,因为这不是为用户 Apache(或 httpd)定义的,为了解决这个问题我修改了初始化使用这个代码块


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

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

【讨论】:

  • FirestoreClient() 只需要 1 个参数而不是 2 个
  • @SANJISHKUMAR 你确定吗?你有机会测试它吗? keyFilePath 记录在 source code
猜你喜欢
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多