Skip to content
Advertisement

Message: Call to private method KreaitFirebaseServiceAccount::fromJsonFile() from context ‘Firebase’

Does this error have anything to do with the Firebase version? If not, how do I solve this issue?

private static function fromJsonFile(string $filePath): self
{
    try {
        $file = new SplFileObject($filePath);
        $json = (string) $file->fread($file->getSize());
    } catch (Throwable $e) {
        throw new InvalidArgumentException("{$filePath} can not be read: {$e->getMessage()}");
    }

    try {
        $serviceAccount = self::fromJson($json);
    } catch (Throwable $e) {
        throw new InvalidArgumentException(sprintf('%s could not be parsed to a Service Account: %s', $filePath, $e->getMessage()));
    }

    return $serviceAccount;
}

Advertisement

Answer

The code you posted is from kreait/firebase-php and shows a private method from the ServiceAccount class that can not be called directly since release 5.0 of the SDK.

Straight from the troubleshooting section in the documentation:

You probably followed a tutorial article or video targeted at a 4.x version, and your code looks like this:

use KreaitFirebaseFactory;
use KreaitFirebaseServiceAccount;

$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google-service-account.json');
$firebase = (new Factory)
    ->withServiceAccount($serviceAccount)
    ->create();

$database = $firebase->getDatabase();

Change it to the following:

use KreaitFirebaseFactory;

$factory = (new Factory)->withServiceAccount(__DIR__.'/google-service-account.json');

$database = $factory->createDatabase();
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement