Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize error 'Unable to create a FirestoreClient: Error rendering...' #19

Closed
ShinoharaTa opened this issue Feb 13, 2020 · 15 comments
Closed

Comments

@ShinoharaTa
Copy link

Hello.

This library is very easy to use. I thank you.

It's working is 2 days ago.
Not working today.

I don't change files .env and firebase_credential.json.
Please let me know if you have any cause.

error message:

Kreait\Firebase\Exception\RuntimeException  : Unable to create a FirestoreClient: Error rendering 'projects/{project=*}/databases/{database=*}': expected binding 'project' to match segment '{project=*}', instead got null
Provided bindings: Array
(
    [project] =>
    [database] => (default)
)


  at /home/mirasapo_admin/wordpress/src/laravel/vendor/kreait/firebase-php/src/Firebase/Factory.php:350
    346|
    347|         try {
    348|             return new FirestoreClient($config);
    349|         } catch (Throwable $e) {
  > 350|             throw new RuntimeException('Unable to create a FirestoreClient: '.$e->getMessage(), $e->getCode(), $e);
    351|         }
    352|     }
    353|
    354|     public function createStorage(array $storageClientConfig = null): Storage

Version:

...
google/auth                           v1.6.1     Google Auth Library for PHP
google/cloud-core                     v1.34.0    Google Cloud PHP shared dependency, providing functionality useful to all components.
google/cloud-firestore                v1.10.0    Cloud Firestore client for PHP
google/cloud-storage                  v1.16.0    Cloud Storage Client for PHP
google/common-protos                  1.1        Google API Common Protos for PHP
google/crc32                          v0.1.0     Various CRC32 implementations
google/gax                            1.2.0      Google API Core for PHP
google/grpc-gcp                       0.1.4      gRPC GCP library for channel management
google/protobuf                       v3.11.1    proto library for PHP
grpc/grpc                             1.25.0     gRPC library for PHP

...
kreait/clock                          1.0.1      A PHP 7.0 compatible clock abstraction
kreait/firebase-php                   4.36.2     Firebase Admin SDK
kreait/firebase-tokens                1.9.2      A library to work with Firebase tokens
kreait/gcp-metadata                   1.0.1      Get the metadata from a Google Cloud Platform environment.
kreait/laravel-firebase               1.2.0      A Laravel package for the Firebase PHP Admin SDK
...
@jeromegamez
Copy link
Member

Hm, this looks like if perhaps the composer autoloader has lost some information - could you try doing a composer dumpautoload and see if that fixes something? And ensure that your credentials file is really unchanged?

I'm just guessing, but the error hints at the project ID not being available, and the project ID is always included in the service account JSON.

@ShinoharaTa
Copy link
Author

Thank you for your prompt reply.
Since an exception was found in Discover.php, I started running after clearing the cache.

Kreait\Firebase\ServiceAccount\Discovery\FromEnvironmentVariable: The environment variable "FIREBASE_CREDENTIALS" is not set.
Kreait\Firebase\ServiceAccount\Discovery\FromEnvironmentVariable: The environment variable "GOOGLE_APPLICATION_CREDENTIALS" is not set.
Kreait\Firebase\ServiceAccount\Discovery\FromGoogleWellKnownFile: The well known file is not readable or invalid
cURL error 6: Could not resolve host: metadata.google.internal (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

We are now thinking about how to respond.
Is there a way to load firebase_credential.json without using Discover.php when doing new Factory () ?

@ShinoharaTa
Copy link
Author

ShinoharaTa commented Feb 14, 2020

I tried $factory = (new Factory())->withServiceAccount('/var/www/server/firebase.json');;

And FIREBASE_CREDENTIALS key is not set.
The process is end, nothing error.
Is this the way it is?

@jeromegamez
Copy link
Member

That's the way it is, yes. As soon as you provide a service account, the factory will not try to auto-discover the credentials after that.

You are not receiving the error in that line, the most common reason for this error is that you are not using the same factory that you initialized. As an example:

$factory = (new Factory())->withServiceAccount('...');

Here, the second line will return a new instance of the factory, the previous `$factory` will be untouched
$factory->withDatabaseUrl('...');

// Correct would have been
$factory = $factory->withDatabaseUrl('...');

@ShinoharaTa
Copy link
Author

Hi, Kreait Team.

Thank you.
With your advice, my project was able to move on.
Thanks for the quick reply and courteous response.

@jeromegamez
Copy link
Member

Oh, I just realized that we are in the context of the Laravel Package 😅 - here you normally shouldn't need to invoke the Factory manually at all.

Could you tell me more about why you are doing new Factory() instead of using app('firebase.<component>') or dependency injection?

@ShinoharaTa
Copy link
Author

I saw this page and wrote the program.

https://firebase-php.readthedocs.io/en/stable/cloud-firestore.html

Now, when I reviewed Readme.md again, it was written. I missed it without noticing.
I will try again!

@ShinoharaTa
Copy link
Author

Hi, jeromegamez.

The program I wrote now looks like.

use Kreait\Firebase;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\WebPushConfig;
            // This code is get Cloud Firestore collections.
            $factory = (new Factory())->withServiceAccount(config('firebase.config'));
            $firestore = $factory->createFirestore();
            $database = $firestore->database();
            $var_list = $database->collection('tokens')->document('tokens')->snapshot();

How can I write this in a form suitable for Laravel without using "new factory ()"?
I am glad that it is also an example to be helpful.

@jeromegamez
Copy link
Member

In Laravel, you could do for example:

$database = app('firebase.firestore')->database();

$var_list = $database->collection('tokens')->document('tokens')->snapshot();

@ShinoharaTa
Copy link
Author

Thank you for your reply.
I tried as you say and it was successful.
Thanks for the quick response.

It was not a problem with your library.
Can I close this issue?

@jeromegamez
Copy link
Member

If you‘re satisfied with the outcome, of course :). Please fell free to create a new issue or ask on Discord if you have further questions!

@chiendv
Copy link

chiendv commented Aug 7, 2021

Today I suddenly got this issue after run config:cache then I figured out that it only read FIREBASE CREDENTIALS in .env not GOOGLE_APPLICATION_CREDENTIALS

@jeromegamez
Copy link
Member

The behaviour should be that it first tries to read from FIREBASE_CREDENTIALS and from GOOGLE_APPLICATION_CREDENTIALS if the former is not set. If both are set, FIREBASE_CREDENTIALS take precedence, even if it's set with an empty value. (

'file' => env('FIREBASE_CREDENTIALS', env('GOOGLE_APPLICATION_CREDENTIALS')),
)

@JHIH-LEI
Copy link

today I have same problem after I ran php artisan config:cache.

After I ran: php artisan config:clear
Problem solve.

@SamMakesCode
Copy link

For anyone who comes after me, I had this issue when using google/cloud-firestore directly. I created a FirestoreServiceProvider which registered a singleton, but I failed to register the service provider in config/app.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants