kreait / firebase-php

Unofficial Firebase Admin SDK for PHP
https://firebase-php.readthedocs.io/
MIT License
2.29k stars 433 forks source link

when project_id not equale to Database Name #537

Closed kaderomeiri closed 3 years ago

kaderomeiri commented 3 years ago

json SDK admin file: { "type": "service_account", "project_id": "project_id_546", "private_key_id": "e63d0e9be01......0339665adbdf", ........ }

My Firebase database name project_id_546-default-rtdb

---------------- ERROR ----------------------------------------------------------

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: GET https://project_id_546.firebaseio.com/child resulted in a 404 Not Found response: in /home/root37/subdomain.domain.com/html/admin/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /home/root37/subdomain.domain.com/html/admin/vendor/guzzlehttp/guzzle/src/Middleware.php(69): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response), NULL, Array, NULL) #1 /home/root37/subdomain.domain.com/html/admin/vendor/guzzlehttp/promises/src/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response)) #2 /home/root37/subdomain.domain.com/html/admin/vendor/guzzlehttp/promises/src/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), NULL) #3 /home/root37/subdomain.domain.com/html/admin/vendor/guzzlehttp/promises/src/TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHtt in /home/root37/subdomain.domain.com/html/admin/vendor/kreait/firebase-php/src/Firebase/Exception/DatabaseApiExceptionConverter.php on line 62

manbuv commented 3 years ago

I have the same problem. If I understood it correctly you have to set :

$factory->withDatabaseUri('https://project_id_546.firebaseio.com');

The only problem is that the function withDatabaseUri does not pass the value.

    public function withDatabaseUri($uri): self
    {
        $factory = clone $this;
        $factory->databaseUri = uri_for($uri);

        return $factory;
    }

I think the following change would work

    public function withDatabaseUri($uri): self
    {
        $this->databaseUri = uri_for($uri);

        return $this;
    }
jeromegamez commented 3 years ago

The correct way to use the factory in general and how to set a database URI is described at https://firebase-php.readthedocs.io/en/5.14.1/setup.html#realtime-database-uri

As you see in the source code that you cited, the withDatabaseUri() method returns a new instance of the factory (as all the other with* methods do.

$factory = $factory->withDatabaseUri('https://project_id_546.firebaseio.com');
arnoldsmyth commented 3 years ago

@jeromegamez I think that the docs could be a little clearer. I had this issue and it took me more time than it should have to figure it out. As I searched for solutions it seemed that most people who were having this issue were using the JSON file downloaded from their Service Account settings and clicking the "Generate new private key" and were not hosting on Google.

When you are using the SDK the docs say to do this: $factory = (new Factory)->withServiceAccount('/path/to/firebase_credentials.json');

However if your Project ID is different to your URL - which mine is - then you must connect like this: $factory = (new Factory)->withServiceAccount('/path/to/firebase_credentials.json')->withDatabaseUri('https://<url>.firebaseio.com');

It would be great if the docs could reflect this so others don't have to search for it.

jeromegamez commented 3 years ago

It's the second position in the setup section of the docs, so fairly accessible early on

Screenshot_20210109-122038.png

but I'll try to make it more visible in a future change, perhaps by adding a "Quick start" or something like that.

A little bit of background: until recently (a couple of months ago) setting the database url manually was an edge case/not needed in 99% of all cases because the first part of the database url was almost always the project ID, and the project ID is included in the Service Account JSON file.

Likewise, being able to have multiple realtime databases is also relatively new, which results in the current process of setting up the factory is not my preferred way of doing things anymore either - but I have to keep it this way until the next major release to not break backward compatibility.

I'll try to "translate" this into better docs in the next days 🤞

jeromegamez commented 3 years ago

Here it is https://github.com/kreait/firebase-php/commit/95a67de20d9ac77ea034fbcfd6cc6971556ea120

https://firebase-php.readthedocs.io/en/latest/

grafik