使用Admin Firebase SDK时,Google服务帐户中缺少字段JSON错误



我正在尝试使用KRAET Admin SDK实现Firebase的实时数据库,以实现PHP 7.0。我已经安装了作曲家和实施所需的所有依赖项,但是我面临一个错误,例如

**Fatal error: Uncaught KreaitFirebaseExceptionInvalidArgumentException:
/var/www/html/cabgotel/google-service-account.json could not be parsed to a Service Account:
The following fields are missing/empty in the Service Account specification:
project_id, client_id, client_email, private_key in /var/www/html/cabgotel/vendor/kreait/firebase-php/src/Firebase/ServiceAccount.php:164 Stack trace:
#0 /var/www/html/cabgotel/firebase.php(42): KreaitFirebaseServiceAccount::fromJsonFile('/var/www/html/c...')
#1 {main} thrown in /var/www/html/cabgotel/vendor/kreait/firebase-php/src/Firebase/ServiceAccount.php on line 164**

我已经下载了服务帐户JSON文件,并将其添加到同一目录中。我什至检查了文件中的值,以了解是否适用于同一firebase项目。

以下是我为此使用的代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
<script src="https://www.gstatic.com/firebasejs/5.8.6/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.8.6/firebase.js"></script>

<script>
  var config = {
    apiKey: "AIzaSyCfGmNSdx5uabtQMGgEjtcz_BL8y_Tm4II",
    authDomain: "auth_domain",
    databaseURL: "my_db_url",
    projectId: "my_project_id",
    storageBucket: "my_project_id_storage_bucket",
    messagingSenderId: "sender_id"
  };
  firebase.initializeApp(config);
</script>
</head>
<body>
</body>
</html>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require __DIR__.'/vendor/autoload.php';
use KreaitFirebase;
use KreaitFirebaseFactory;
use KreaitFirebaseServiceAccount;
// This assumes that you have placed the Firebase credentials in the same directory
// as this PHP file.
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google-service-account.json');
$firebase = (new Factory)
    ->withServiceAccount($serviceAccount)
    ->withDatabaseUri('https://my_project.firebaseio.com')
    ->create();
$database = $firebase->getDatabase();
$newPost = $database
    ->getReference('https://my_project.firebaseio.com')
    ->push([
        'lan' => '10.77',
        'lat' => '88.99'
    ]);
$newPost->getKey(); // => -KVr5eu8gcTv7_AHb-3-
$newPost->getUri(); // => https://my-project.firebaseio.com/blog/posts/-KVr5eu8gcTv7_AHb-3-
$newPost->getChild('lan')->set('11.211');
$newPost->getValue(); // Fetches the data from the realtime database
$newPost->remove();

请帮助。

有两个文件,它们完全不同:

  1. google-services.json用于您发送给用户的Android应用程序,仅包含Firebase Client SDK使用的配置数据,用于在Google的服务器上找到该项目。

  2. 服务帐户JSON,其中包含一个允许代码管理(无限制地)访问您项目的公共密钥对。

错误消息给我读取,例如代码 can 查找文件,但是 chere 在文件中找到一些必需的字段。可以确保您从firebase控制台中的"服务帐户"选项卡下载服务帐户JSON,如文档中所示的有关将firebase添加到应用程序的文档中。

最新更新