firebase-php: Error creating resource, Connection timed out

1. Which version of kreait/firebase-php are you using?

kreait/firebase-php@4.1.2

guzzlehttp/guzzle@6.3.0

2. Which version of PHP are you using?

PHP 7.0.25-0ubuntu0.16.04.1 (cli) ( NTS )

3. What’s the issue?

My code suddently stopped working with kreait/firebase-php@3.1 – probably because of Firebase changes/deprecations – so I thought I’ll do an update. I updated to 4.1.2 generated new Firebase Service Account credentials. Then I replaced the deprecated asUserWithClaims method with asUser instead. On my local machine everything worked fine, but online I can’t seem to be able to request the data.

After surrounding my code (see section 4) with a try catch block, i receive the following Exception:

Exception:
Error creating resource: [message]
fopen(https://PROJECT_ID.firebaseio.com/PATH_TO_RESSOURCE.json?
auth_variable_override=%7B%22uid%22%3A%22MY_ADMIN_UID%22%7D): failed to open stream:
Connection timed out [file]
/var/www/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php [line] 324

4. Code that lead to the issue

NOTE that I’ve replaced some sensitive data:

<?php

require_once 'vendor/autoload.php';
require_once 'constants.php';

// debugging
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;

try {
  // Firebase Admin SDK
  $serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/__my_private_credentials__.json');
  $firebase = (new Factory)
    ->withServiceAccount($serviceAccount)
    ->asUser(MY_ADMIN_UID) // from constants.php
    ->create();
  $database = $firebase->getDatabase();

  $projectPath = PATH_TO_RESSOURCE; // from constants.php
  $projectRef = $database->getReference($projectPath);
  $projectData = json_decode( json_encode($projectRef->getValue()) );

  echo '<pre>';
  var_dump($projectData);
  echo '</pre>';

} catch(Exception $e) {
  echo 'Exception:<br>';
  echo $e->getMessage();
}

exit;

Guesses

I can only guess what’s wrong here… Maybe because we use SSL on the server ? Or do I require an API key from firebase and use withServiceAccountAndApiKey instead ?

What are your guesses ? It would be awesome to get a lead on this issue, since we use this in production…

Thanks in advance !

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (8 by maintainers)

Most upvoted comments

Depends - you don’t need the check for your requests through the Firebase SDK because the security rules don’t apply to the service account - but as far as I see, if you change it to auth != null only, any authenticated user can read an write everything.

Not related to the issue at hand:

The api key is not needed anymore (it probably never was 😅), so you can safely remove it from your environment(s).

Also, just to make sure that we are on the same page: if your user with with the MY_ADMIN_UID is allowed to do anything on your realtime database, you could consider just dropping it altogether, because thanks to the service account credentials, you have admin permissions by default.