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

Unable to parse URI #228

Closed
digitalStyx opened this issue Jul 24, 2018 · 9 comments
Closed

Unable to parse URI #228

digitalStyx opened this issue Jul 24, 2018 · 9 comments

Comments

@digitalStyx
Copy link

digitalStyx commented Jul 24, 2018

I use version 4.13 with PHP 7.2.5 .

I have a project name in the form
"project_id": "example.com:api-project-xxxxxx",

and when I use example

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

I got error

Fatal error: Uncaught InvalidArgumentException: Unable to parse URI: https://example.com:api-project-xxxxxx.firebaseio.com in /export/www/DEVEL/example.com/vendor/guzzlehttp/psr7/src/Uri.php on line 78

What can be? example.com:api-project-xxxxxx is a valid name form for google

@jeromegamez
Copy link
Member

If the project ID in your service account file doesn't match the database URI, you have to specify the database URI as documented in https://firebase-php.readthedocs.io/en/latest/setup.html#custom-database-uri :

$firebase = (new Factory())
    ->withServiceAccount($serviceAccount)
    ->withDatabaseUri(...)
    ->create();

You can find the URI for your database by navigating to the realtime database in the Firebase Web console and copying the value displayed there - it should be something in the form of https://xxxxxx.firebaseio.com/.

@digitalStyx
Copy link
Author

digitalStyx commented Jul 24, 2018

I don't understand. I use generated file from the google console panel. How is it possible that Google generates incorrect data for me? Problem is during parsing JSON. I have very old project, and my project_id is not understandable for your code.

I have databaseUri: https://example.com:api-project-xxx.firebaseio.com and it is correct URI for google but it's not correct for this library and parse_url (PHP). The problem is in sign ":" .

I changed GuzzleHttp\Psr7/Uri for

 public function __construct($uri = '')
    {
        // weak type check to also accept null until we can add scalar type hints
        if ($uri != '') {
            if ($uri == "https://example.com:api-project-xxx.firebaseio.com") {
                $parts[0] = "https";
                $parts[1] = "example.com:api-project-xxx.firebaseio.com";
            } else {
                $parts = parse_url($uri);
                if ($parts === false) {
                    throw new \InvalidArgumentException("Unable to parse URI: $uri");
                }
            }
            $this->applyParts($parts);
        }
    }

and it's work great, but it can't be. I think that not all URI for database is valid with RFC (parse_url), and it's a mistake to valid in this way

    protected function getDatabaseUriFromServiceAccount(ServiceAccount $serviceAccount): UriInterface
    {
        return uri_for(sprintf(self::$databaseUriPattern, $serviceAccount->getProjectId()));
    }

@digitalStyx
Copy link
Author

I see that firebase change url to database from:

https://example.com:api-project-xxx.firebaseio.com/
to
https://example-com-api-project-xxx.firebaseio.com/

I think that something like that must be implemented for for project_id in form: example.com:api-project-xxx

@jeromegamez
Copy link
Member

jeromegamez commented Jul 24, 2018

The service account JSON file that Google generates is fine. It includes the project ID which can have any form, apperently.

In most projects, the project ID is identical to the subdomain on firebaseio.com, so a project id foo would point to the Firebase Database URI https://foo.firebaseio.com.

That's why the SDK by default uses the project ID from the service account file and uses it for the database URI.

In some project, the project ID is not identical to the subdomain on firebaseio.com, and that's why the ->withDatabaseUri() method exists, so that you can override the value.

If you are sure that you are using the database URI displayed at https://console.firebase.google.com/u/0/project/_/database/_/data (after clicking the link, select your project) and the displayed URI indeed is https://example.com:api-project-xxx.firebaseio.com, you should contact Google and tell them about this issue, because this is not a valid URI, which you can check with a cURL command in your terminal:

❯ curl -I "https://example.com:api-project-xxx.firebaseio.com/"
curl: (3) Port number ended with 'a'

This SDK, Guzzle and any HTTP Client need a valid URL to be able to connect to it. Even if you change Guzzle's source code, you will not be able to connect.

@jeromegamez
Copy link
Member

I see that firebase change url to database from:

https://example.com:api-project-xxx.firebaseio.com/
to
https://example-com-api-project-xxx.firebaseio.com/

I think that something like that must be implemented for for project_id in form: example.com:api-project-xxx

This is something you can do when initializing the SDK:

$firebase = (new Factory())
    ->withServiceAccount($serviceAccount)
    ->withDatabaseUri('https://example-com-api-project-xxx.firebaseio.com')
    ->create();

@digitalStyx
Copy link
Author

Yep, if I use withDatabaseUri with my URI, then it's work great.

But I think, that library should do this.

anyway, thanks @jeromegamez .

@jeromegamez
Copy link
Member

I will try to look for a reliable specification by Google/Firebase - if there is one, I will 👍

@jeromegamez
Copy link
Member

Hello again @digitalStyx 😅

If you upgrade to the latest release, you should be able to remove the withDatabaseUri(...) line again :). Cheers!

@lock
Copy link

lock bot commented Jul 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants