Pregunta

I'm trying to write to a firebase with php using REST and i keep getting permission denied

I'm using the test file like this:

$url = 'https://my.firebase.url/';
$atoken = '?auth=MY FIREBASE SECRET';

$fb = new fireBase($url);

$todos = array(
        'name' => 'Pick the milk',
        'priority' => 1
        );

$todoPath = '/test/test';

printf("Database: %s\n", $url);

printf("Sending data to %s\n", $todoPath);
$response = $fb->set($todoPath, $todos.$atoken);
printf("Result: %s\n", $response);

printf("Reading data from %s\n", $todoPath);
$response = $fb->get($todoPath);
printf("Result: %s\n", $response);

I have rules set up that only a specific authorized user "me" the admin can write to the the firebase but anyone can read from in. This was fine when the code was in Javascript because I would log in and be authorized. But now it's in php and I thought the "SECRET" would make that work or at lease I was lead to believe that by the firebase DOCS.

So what am I doing wrong?

Thank you!

UPDATE:

So I changed the:

$atoken = '?auth=MY FIREBASE SECRET';

to be

$atoken = '.json?auth=MY FIREBASE SECRET';

and I put it here:

printf("Sending data to %s\n", $todoPath.$atoken);
    $response = $fb->set($todoPath.$atoken, $todos);
    printf("Result: %s\n", $response);

    printf("Reading data from %s\n", $todoPath.$atoken);
    $response = $fb->get($todoPath.$atoken);
    printf("Result: %s\n", $response);

Now I get this Error:

04Database: https://MYDATABASE/ Sending data to /test/test.json?auth=MY FIREBASE SECRET Result: { "error" : "invalid_token: Could not parse auth token." } Reading data from /test/test.json?auth=MY FIREBASE SECRET Result: { "error" : "invalid_token: Could not parse auth token." } 
¿Fue útil?

Solución

It looks like that unofficial PHP / Firebase library does not currently support authentication, (see https://github.com/ktamas77/firebase-php/issues/1 and stackoverflow.com/questions/15953505/firebase-php-curl-authentication/15953974.

I would recommend forking the project and adding a .auth method which hangs on to your token and automatically adds it to each request after the addition of .json on https://github.com/ktamas77/firebase-php/blob/master/firebaseLib.php#L63. Don't forget to submit a pull request to improve that library for others!

Otros consejos

The REST spec looks like it allows this. I read it the same as you.

What is the exact error you're getting? Have you verified in Forge's simulator that you can perform these exact ops?

As for your PHP code, one thing stands out:

You have $response = $fb->set($todoPath, $todos.$atoken);

You are concatenating $todos (an array) with $atoken (a string), which isn't going to work. Did you mean to use http_build_query or json_encode?

Looking over the firebase-php lib, you shouldn't be making those into a string anyway; it looks like you should be doing this:

$todos = array(
        'name' => 'Pick the milk',
        'priority' => 1,
        'auth' => MY_FIREBASE_SECRET
        );
$response = $fb->set($todoPath, $todos);

Or maybe

$todos = array(
        'name' => 'Pick the milk',
        'priority' => 1
        );
$response = $fb->set($todoPath.$atoken, $todos);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top